没有函数的JavaFX stage.setOnCloseRequest?

mal*_*lex 2 javafx javafx-8

这是我的Start-Methode.首先,我创建一个舞台,并设置标题和场景.如果有人想关闭window-close-btn [X]上的窗口,我想创建一个对话框.我以为我会用setOnCloseRequest() - 函数捕获这个事件.但我仍然可以关闭运行时打开的所有阶段.

@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);
}
Run Code Online (Sandbox Code Playgroud)

是否还有其他功能可以捕获窗口事件?或者在primaryStage上运行CloseRequest是不合逻辑的,我用平台读取了一些内容(但我不知道是否有必要解决我的问题)?

Jam*_*s_D 9

onCloseRequest处理程序中,调用event.consume();.

这将阻止初级阶段结束.

primaryStage.show();从取消按钮的处理程序中删除调用,并primaryStage.hide();在"确定"按钮的处理程序中添加一个调用.