我的圈子不会显示。我在这里想念什么?

问题描述 投票:0回答:1

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

我执行了与此类似的程序,以自动绘制根据舞台/窗口调整大小的图像,我使用了所有相同的技术,但是该项目不包含事件处理。

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);
    }
}

没有错误消息。

javafx-8
1个回答
0
投票

问题是,您实例化了两个ClickPane对象,一个在start方法外部,另一个在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);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.