我正在学习 javafx,我希望在关闭后单击 X 的窗口时出现警告。我知道在我在窗口中间创建的按钮中执行此操作,但我不知道如何控制用户何时按 X 关闭程序。谢谢
最佳答案
你不应该关注 X,而应该关注像这样的常见关闭请求:
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
// consume event
event.consume();
// show close dialog
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Close Confirmation");
alert.setHeaderText("Do you really want to quit?");
alert.initOwner( primaryStage);
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK){
Platform.exit();
}
}
});
关于java - 禁用阶段按钮 X,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29403706/