如何在javafx中将EventListener分配给Path?

时间:2015-11-20 12:25:30

标签: javafx event-handling

我有这段代码无法正常工作。 我想为用户在广场内点击时设置一个监听器 单击时,不会显示弹出窗口或“单击”消息 在广场内。

我错过了什么?

此方法位于Coords类中。

public static void drawMyShape(final GraphicsContext ctx) {
    Path path = new Path();
    MoveTo mT = new MoveTo();
    LineTo lT[] = new LineTo[4];


    mT.setX(200.0);
    mT.setY(200.0);
    lT[0] = new LineTo(400.0, 200.0);
    lT[1] = new LineTo(400.0, 400.0);
    lT[2] = new LineTo(200.0, 400.0);
    lT[3] = new LineTo(200.0, 200.0);
    path.setStroke(Color.BEIGE);
    path.getElements().addAll(mT, lT[0], lT[1], lT[2], lT[3]);

    path.setOnMouseClicked(new EventHandler<MouseEvent>() {
        @Override
        public void handle(MouseEvent event) {
            final Stage dialog = new Stage();
            dialog.initModality(Modality.APPLICATION_MODAL);
            dialog.initOwner(Main.prim_stage);
            VBox box = new VBox(20);
            box.getChildren().add(new Text("Hey"));
            Scene s = new Scene(box, 300, 200);
            dialog.setScene(s);
            dialog.show();
            System.out.println("Clicked");
        }
    });

    ctx.setLineWidth(4.0);
    ctx.beginPath();
    ctx.moveTo(mT.getX(), mT.getY());
    for (int i = 0; i < lT.length; i++) {
        ctx.lineTo(lT[i].getX(), lT[i].getY());
    }
    ctx.closePath();
    ctx.stroke();
}

由用户编辑推荐。

所以他是主要的节目:

public class Main extends Application {
    public static Pane root;
    private static Canvas main_canvas;
    private static GraphicsContext ctx;
    private static Rectangle2D bounds;
    private static Scene scene;
    public static Stage prim_stage;

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Switzerland Advertising");
        initElements(primaryStage);
        Coords.drawMyShape(ctx);
        primaryStage.show();
        System.out.println("Launched");
    }
    public static void main(String[] args) {
        launch(args);
    }
}

所有内容都在以下函数中实现,该函数正常工作并显示一个全屏应用程序,其中包含一个画布和一个绘制在其中的正方形(图像在底部)。

private void initElements(final Stage primaryStage) {
    prim_stage = primaryStage;
    // ----------------------------------------
    bounds = Screen.getPrimary().getVisualBounds();
    double w = bounds.getWidth();
    double h = bounds.getHeight();
    // ----------------------------------------
    // init elements of scene
    root = new Pane();
    main_canvas = new Canvas(w, h);
    // ----------------------------------------
    // init scene elements
    scene = new Scene(root, w, h);
    primaryStage.setX(bounds.getMinX());
    primaryStage.setY(bounds.getMinY());
    primaryStage.setWidth(w);
    primaryStage.setHeight(h);
    primaryStage.setScene(scene);
    // ----------------------------------------
    ctx = main_canvas.getGraphicsContext2D();
    // set elements in main pane
    root.getChildren().add(main_canvas);
    // ----------------------------------------
} 

那么每当我在画布上绘制的区域内单击时,如何使弹出窗口出现? This is the program

1 个答案:

答案 0 :(得分:0)

您的路径只是您方法中的局部变量。它必须附加到场景图以获取事件。但是当你将它附加到场景图时,在画布上绘制相同的路径也没有多大意义。