我的圈子不会显示。我在这里错过了什么?

My circles won't show. What am I missing here?

在 Javafx 中,我试图创建一个窗格,我可以在其中通过鼠标单击事件添加点。当您单击窗格时,鼠标位置应出现一个圆圈。正在创建圆圈,因为我在控制台中跟踪它们,但它们没有显示在图形中。

我做了一个与此类似的程序,它自动绘制了一个使用 stage/window 调整大小的图像,我使用了所有相同的技术,但该项目不包括事件处理。

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.scene.paint.Color;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;


public class ClickToShape extends Application {
    private ClickPane clickPane = new ClickPane();

    @Override
    public void start(Stage primaryStage) throws Exception {
        Pane clickPane = new ClickPane();

        clickPane.setOnMouseClicked(new ClickHandler());


        // create the scene
        Scene clickScene = new Scene(clickPane, 500, 500);
        // set up the window/stage
        primaryStage.setTitle("Click To Draw");
        primaryStage.setScene(clickScene); // add the scene to the stage
        primaryStage.show(); // fire it off

    }

    public static void main(String[] args) {
        launch(args);
    }

    class ClickHandler implements EventHandler<MouseEvent> {
        @Override
        public void handle(MouseEvent e) {
            System.out.println("MouseEvent occured");
            clickPane.addPoint(e.getX(), e.getY());
        }

    }

}

class ClickPane extends Pane{
    private ArrayList<Circle> points = new ArrayList<Circle>();
    private Color color1 = Color.BLACK;


    public void addPoint(double x, double y) {
        System.out.println("A new point function ran");
        Circle newPoint = new Circle (x, y, 300, color1 );
        System.out.println(newPoint.toString());
        points.add(newPoint);
        getChildren().clear();
        getChildren().add(newPoint);
    }
}

没有错误消息。

问题是你实例化了两个 ClickPane 对象,一个在 start 方法之外,另一个在 start 方法内部,你将第二个添加到场景中,但使用第一个来添加点,这就是点不存在的原因'出现在你的场景中

你可以做的是删除启动方法中的第一行,这样应用程序将使用相同的实例来触发事件,就像添加到场景中一样,代码如下所示

import java.util.ArrayList;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;


public class ClickToShape extends Application {
    private ClickPane clickPane = new ClickPane();

    @Override
    public void start(Stage primaryStage) throws Exception {

        clickPane.setOnMouseClicked(new ClickHandler());

        // create the scene
        Scene clickScene = new Scene(clickPane, 500, 500);
        // set up the window/stage
        primaryStage.setTitle("Click To Draw");
        primaryStage.setScene(clickScene); // add the scene to the stage
        primaryStage.show(); // fire it off

    }

    public static void main(String[] args) {
        launch(args);
    }

    class ClickHandler implements EventHandler<MouseEvent> {
        @Override
        public void handle(MouseEvent e) {
            System.out.println("MouseEvent occured");
            clickPane.addPoint(e.getX(), e.getY());
        }

    }

}

class ClickPane extends Pane{
    private ArrayList<Circle> points = new ArrayList<Circle>();
    private Color color1 = Color.BLACK;

    public void addPoint(double x, double y) {
        System.out.println("A new point function ran");
        Circle newPoint = new Circle (x, y, 10, color1 );
        System.out.println(newPoint.toString());
        points.add(newPoint);
        getChildren().setAll(newPoint);
    }
}