JavaFX create alert and get result(JavaFX 创建警报并获得结果)
问题描述
对于我的 CS 课程,他们要求我们使用 JavaFX 警报.我可以发出警报,但是如何获取单击了哪个按钮?获取这些数据的最佳方法是什么?
For my CS class they require us to use JavaFX alerts. I can make an alert appear, but how do I get what button was clicked? What would be the best way to go about getting this data?
如果可能的话,我想让它有一个下拉面板,当用户选择和选项时,警报会关闭并打印用户选择的内容.
Also if possible, I'd like to make it have a drop down panel and when the user selects and option the alert closes and prints what the user selected.
这是我拥有的一些示例代码.当我单击其中一个按钮时,它只会关闭对话框.
Here's some example code that I have. When I click one of the buttons, it just closes the dialog.
Alert a = new Alert(AlertType.NONE, "Promote pawn to:", new ButtonType("Queen"), new ButtonType("Rook"));
a.setTitle("Title");
a.setHeaderText("My header text");
a.setResizable(true);
a.setContentText("Content text");
a.showAndWait();
谢谢,
推荐答案
你可以做
ButtonType queen = new ButtonType("Queen");
ButtonType rook = new ButtonType("Rook");
Alert a = new Alert(AlertType.NONE, "Promote pawn to:", queen, rook);
a.setTitle("Title");
a.setHeaderText("My header text");
a.setResizable(true);
a.setContentText("Content text");
a.showAndWait().ifPresent(response -> {
if (response == queen) {
// promote to queen...
} else if (response == rook) {
// promote to rook...
}
});
这篇关于JavaFX 创建警报并获得结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JavaFX 创建警报并获得结果


基础教程推荐
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01