JavaFX stage.setOnCloseRequest是否不带函数?
这是我的出发点。首先,我创建一个舞台,设置一个标题和一个场景。我想创建一个对话框,如果有人想关闭窗口上的窗口关闭btn[X]。我想我会用setOnCloseRequest()函数捕捉这个事件。但我仍然可以关闭运行时打开的所有阶段JavaFX stage.setOnCloseRequest是否不带函数?,javafx,javafx-8,Javafx,Javafx 8,这是我的出发点。首先,我创建一个舞台,设置一个标题和一个场景。我想创建一个对话框,如果有人想关闭窗口上的窗口关闭btn[X]。我想我会用setOnCloseRequest()函数捕捉这个事件。但我仍然可以关闭运行时打开的所有阶段 @Override public void start(final Stage primaryStage) throws Exception { primaryStage.setTitle("NetControl"); primaryStage.setS
@Override
public void start(final Stage primaryStage) throws Exception {
primaryStage.setTitle("NetControl");
primaryStage.setScene(
createScene(loadMainPane())
);
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(final WindowEvent event) {
//Stage init
final Stage dialog = new Stage();
dialog.initModality(Modality.APPLICATION_MODAL);
// Frage - Label
Label label = new Label("Do you really want to quit?");
// Antwort-Button JA
Button okBtn = new Button("Yes");
okBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
dialog.close();
}
});
// Antwort-Button NEIN
Button cancelBtn = new Button("No");
cancelBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
primaryStage.show();
dialog.close();
}
});
}
});
primaryStage.show();
}
private Pane loadMainPane() throws IOException {
FXMLLoader loader = new FXMLLoader();
Pane mainPane = (Pane) loader.load(
getClass().getResourceAsStream(ContentManager.DEFAULT_SCREEN_FXML)
);
MainController mainController = loader.getController();
ContentManager.setCurrentController(mainController);
ContentManager.loadContent(ContentManager.START_SCREEN_FXML);
return mainPane;
}
private Scene createScene(Pane mainPane) {
Scene scene = new Scene(mainPane);
setUserAgentStylesheet(STYLESHEET_MODENA);
return scene;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Application.launch(args);
}
@覆盖
public void start(final Stage primaryStage)引发异常{
primaryStage.setTitle(“网络控制”);
初级阶段(
CreateSecene(loadMainPane())
);
setOnCloseRequest(新的EventHandler()){
@凌驾
公共无效句柄(最终WindowEvent事件){
//阶段初始化
最终阶段对话框=新阶段();
dialog.initmodel(model.APPLICATION\u model);
//破损标签
Label Label=新标签(“您真的想退出吗?”);
//紫草钮扣
按钮okBtn=新按钮(“是”);
okBtn.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
dialog.close();
}
});
//金雀花
按钮取消BTN=新按钮(“否”);
cancelBtn.setOnAction(新的EventHandler(){
@凌驾
公共无效句柄(ActionEvent事件){
primaryStage.show();
dialog.close();
}
});
}
});
primaryStage.show();
}
私有窗格loadMainPane()引发IOException{
FXMLLoader=新的FXMLLoader();
窗格主窗格=(窗格)loader.load(
getClass().getResourceAsStream(ContentManager.DEFAULT\u SCREEN\u FXML)
);
MainController MainController=loader.getController();
ContentManager.setCurrentController(主控制器);
ContentManager.loadContent(ContentManager.START\u SCREEN\u FXML);
返回主窗格;
}
专用场景创建场景(窗格主窗格){
场景=新场景(主窗格);
setUserAgentStylesheet(样式表_MODENA);
返回场景;
}
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
应用程序启动(args);
}
是否有其他函数用于捕获窗口事件?
或者在primaryStage上运行CloseRequest不符合逻辑吗?我在平台上读到了一些东西(但我不知道这对我的问题是否有必要)?在
onCloseRequest
处理程序中,调用event.consume()代码>
这将阻止主级关闭
删除primaryStage.show()代码>从cancel按钮的处理程序调用,并添加对primaryStage.hide()的调用代码>在OK按钮的处理程序中。感谢您的快速回答!!它解决了我的问题;)