我需要在应用程序关闭之前显示警报确认(X 按下按钮)
public void start(Stage primaryStage) throws Exception {
this.primaryStage = primaryStage;
...
primaryStage.setOnHiding(event -> {
System.out.println("hidding");
Alert alert = new Alert(Alert.AlertType.CONFIRMATION, Lang.getString("exit_confirmation"));
Toolkit.getDefaultToolkit().beep();
Optional<ButtonType> result = alert.showAndWait();
if (result.isPresent() && result.get() != ButtonType.OK) {
return;
//don't close stage
}
});
}
Run Code Online (Sandbox Code Playgroud)
但无论如何primaryStage都是关闭的。我如何编码才能仅在ButtonType.OK按下时退出应用程序?对不起我的英语不好
最好先用谷歌搜索你的问题,因为你可以找到更好的解释,但这是你的答案
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
//your code goes here
//this line cancel the close request
event.consume();
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1566 次 |
| 最近记录: |