本文介绍了没有函数的JavaFX stage.setOnCloseRequest?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is my Start-Methode. First i create a stage and set a title and a scene. And i wanna create a dialog if someone wants to close the window on the window-close-btn [X]. i thought i will catch this event with the setOnCloseRequest()-function. But i still can close all stages i open while runtime.

@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);
}

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

Are there any other functions to catch the window-events?or isnt it logical to run the CloseRequest on the primaryStage, i read something with platforms (but i dont know if it necessary for my problem)?

推荐答案

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

In the onCloseRequest handler, call event.consume();.

这将阻止初级阶段结束。

That will prevent the primary stage from closing.

从取消按钮的处理程序中删除 primaryStage.show(); 调用并添加对<$ c的调用$ c> primaryStage.hide(); 在OK按钮的处理程序中。

Remove the primaryStage.show(); call from the cancel button's handler and add a call to primaryStage.hide(); in the OK button's handler.

这篇关于没有函数的JavaFX stage.setOnCloseRequest?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 23:45