javafx中的对话
我想在用户单击关闭按钮时实现一个对话警报。选择“是”,他们确实想离开,不想离开javafx中的对话,java,javafx,Java,Javafx,我想在用户单击关闭按钮时实现一个对话警报。选择“是”,他们确实想离开,不想离开 Button button = new Button("Exit"); gridPane.add(button, 12, 12); button.setOnAction(e ->{ primaryStage.close(); }); 我该怎么做呢?你有一篇很好的文章解释了et,并给出了一些例子 您需要的是: 按钮。设置操作(e-> 警报警报=新警报(警报类型.确认);
Button button = new Button("Exit");
gridPane.add(button, 12, 12);
button.setOnAction(e ->{
primaryStage.close();
});
我该怎么做呢?你有一篇很好的文章解释了et,并给出了一些例子 您需要的是:
按钮。设置操作(e->
警报警报=新警报(警报类型.确认);
alert.setTitle(“退出应用程序”);
alert.setHeaderText(“退出应用程序”);
setContentText(“您真的想退出吗?”);
可选结果=alert.showAndWait();
if(result.get()==ButtonType.OK){
primaryStage.close();
Platform.exit();
系统出口(0);
}否则{
//…用户选择了取消或关闭对话框
}
});
你有一篇很好的文章解释了et,并给出了一些关于et的例子
您需要的是:
按钮。设置操作(e->
警报警报=新警报(警报类型.确认);
alert.setTitle(“退出应用程序”);
alert.setHeaderText(“退出应用程序”);
setContentText(“您真的想退出吗?”);
可选结果=alert.showAndWait();
if(result.get()==ButtonType.OK){
primaryStage.close();
Platform.exit();
系统出口(0);
}否则{
//…用户选择了取消或关闭对话框
}
});
尝试以下方法:
Button button = new Button("Exit");
gridPane.add(button, 12, 12);
button.setOnAction(e ->{
if(confirmDialog(
"Sure you want to quit?",
"Sure you want to quit?",
"We're really closing - click yes to quit, no to stay in the app")
) {
primaryStage.close();
}
});
...
public boolean confirmDialog(String title, String headerText, String message) {
Alert alert = new Alert(AlertType.CONFIRMATION, message, ButtonType.YES, ButtonType.NO);
alert.initModality(Modality.APPLICATION_MODAL);
alert.initOwner(scene); //scene must be accessible as a field
alert.setTitle(title);
alert.setHeaderText(headerText);
ButtonType result = alert.showAndWait().orElse(ButtonType.NO);
return ButtonType.YES==result;
}
尝试以下方法:
Button button = new Button("Exit");
gridPane.add(button, 12, 12);
button.setOnAction(e ->{
if(confirmDialog(
"Sure you want to quit?",
"Sure you want to quit?",
"We're really closing - click yes to quit, no to stay in the app")
) {
primaryStage.close();
}
});
...
public boolean confirmDialog(String title, String headerText, String message) {
Alert alert = new Alert(AlertType.CONFIRMATION, message, ButtonType.YES, ButtonType.NO);
alert.initModality(Modality.APPLICATION_MODAL);
alert.initOwner(scene); //scene must be accessible as a field
alert.setTitle(title);
alert.setHeaderText(headerText);
ButtonType result = alert.showAndWait().orElse(ButtonType.NO);
return ButtonType.YES==result;
}
使用阶段的
onCloseRequest
事件,使用窗口的X
按钮关闭窗口:
private static boolean confirmClose() {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setContentText("Do you really want to close the app?");
return alert.showAndWait().orElse(ButtonType.CANCEL) == ButtonType.OK;
}
请注意,以编程方式关闭窗口时不会触发此事件。在这种情况下,您需要自己请求用户确认:
button.setOnAction(evt -> {
if (confirmClose()) {
primaryStage.close();
}
});
使用阶段的
onCloseRequest
事件,使用窗口的X
按钮关闭窗口:
private static boolean confirmClose() {
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setContentText("Do you really want to close the app?");
return alert.showAndWait().orElse(ButtonType.CANCEL) == ButtonType.OK;
}
请注意,以编程方式关闭窗口时不会触发此事件。在这种情况下,您需要自己请求用户确认:
button.setOnAction(evt -> {
if (confirmClose()) {
primaryStage.close();
}
});
你在找这样的东西吗?你在找这样的东西吗?不要这样做来获取
可选值。改用orElse
。这样可以避免为要检索的值创建容器。(AtomicBoolean
尤其糟糕,因为它还负责同步值。):ButtonType结果=alert.showAndWait().orElse(ButtonType.NO)代码>谢谢@fabian,已编辑。不要这样做来获取可选值。改用orElse
。这样可以避免为要检索的值创建容器。(AtomicBoolean
尤其糟糕,因为它还负责同步值。):ButtonType结果=alert.showAndWait().orElse(ButtonType.NO)代码>谢谢@fabian,编辑。