java - Scenebuilder/JavaFX 多边形鼠标事件 onClick
问题描述
链接到 scenebuilder 和一些 Java 代码:https ://imgur.com/a/FOr1Mag
本质上,导航和图片将根据人的去向/面对的地方而改变。
我有多边形作为 D-pad 箭头,我希望能够检测到一个人何时点击它们。“向上”箭头多边形 ID 是“向前”
,我读到 forward.onMouseClickedProperty.addListener() 或其他可以使用的东西,但是当我查找“javafx 多边形鼠标事件”时,我不知道如何在我的项目中实现。
谁能告诉我如何设置 forward.onMouseClickedProperty.addListener()?谢谢!
解决方案
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!"));
}
}
推荐阅读
- python - 如何在python中将json转换为树
- node.js - 如何通过 nodejs 在 S3 中创建 Excel 文件?
- c# - 解析 Enum 时出现 Automapper 错误:无法将带有语句体的 lambda 表达式转换为表达式树
- wix - 升级期间出现 Wix 安装程序错误 - 安装程序必须更新系统运行时无法更新的文件或服务
- azure-devops - Azure DevOps 将文件上传/提供到 Web 扩展
- python - 使用 DynamoDB transact_write_items 如何对现有项目进行 ConditionCheck 并在 ConditionCheck 为 True 时放置新项目?
- python - 百分比回归 - r、python 和 matlab 中的不同结果
- android - 有没有办法捕捉调用的值事件监听器
- android - AppCenter 推送到 Xamarin.Forms Android 无震动
- django - 使用 Django 加入迁移表