【问题标题】:Scenebuilder/JavaFX polygons mouse event onClickScenebuilder/JavaFX 多边形鼠标事件 onClick
【发布时间】:2018-11-19 11:05:12
【问题描述】:

scenebuilder 和一些 Java 代码的链接:https://imgur.com/a/FOr1Mag

基本上,导航和图片会根据人的去向/面对的地方而改变。

我有多边形作为方向键箭头,我希望能够检测到有人点击它们的时间。 “向上”箭头多边形 ID 为“向前”
我读到了 forward.onMouseClickedProperty.addListener() 或可以使用的东西,但是当我查找“javafx 多边形鼠标事件”时,我不知道如何在我的项目中实现。

谁能告诉我如何设置forward.onMouseClickedProperty.addListener()? 谢谢!

【问题讨论】:

    标签: java eclipse javafx scenebuilder eclipse-photon


    【解决方案1】:
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.StackPane;
    import javafx.scene.shape.Polygon;
    import javafx.stage.Stage;
    
    public class ClickablePolygonApp extends Application {
    
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage stage) throws Exception {
            Polygon polygon = new Polygon();
            polygon.getPoints().addAll(new Double[] {
                    0., 80.,
                    80., 80.,
                    40., 20.
            });
    
            StackPane stackPane = new StackPane(polygon);
            stackPane.setPrefSize(400., 400.);
    
            stage.setScene(new Scene(stackPane));
            stage.show();
    
            polygon.setOnMouseClicked(mouseEvent -> System.out.println("1st way to handle Click!"));
            polygon.addEventHandler(MouseEvent.MOUSE_CLICKED, mouseEvent -> System.out.println("2nd way to handle click!"));
        }
    }
    

    【讨论】:

    • 如果 id 为“forward”,我如何将它与 Scenebuilder 中的特定多边形联系起来?谢谢
    • new FXMLLoader(getClass().getClassLoader().getResource("yourFxml")).setController(yourControllerClass); yourControllerClass 应该有字段@FXML Polygon forward。现在已经绑定了。
    猜你喜欢
    • 2015-07-09
    • 2022-07-27
    • 1970-01-01
    • 2014-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-15
    相关资源
    最近更新 更多