java - 当我通过单击窗口的 [X] 红色按钮关闭屏幕时会调用哪个事件?

标签 java javafx window

如果我通过按右上角的 [X] 按钮直接关闭窗口,Java FX 中是否存在任何事件处理程序?在这种情况下哪些事件会被触发?到目前为止,没有任何效果,无论是 setOnHiding 还是 setOnCloseRequest() 我想要的是,当我关闭(通过单击 [X] 按钮)showLogin Screen 时,showSplashScreen 也应该关闭。

请帮忙。

在此处附加我的代码。

@Override
    public void start(Stage primaryStage) {

      ....
      ......
      .......
        this.primaryStage = primaryStage;
        if (userCredentials == null) {
            showSplashScreen();
            showLoginScreen();
        } else {
            showAppointmentScreen();
        }
    }


private void showSplashScreen(){
        primaryStageSplash = new Stage();

        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/SplashScreen.fxml"), bundle);
        primaryStageSplash.setResizable(false);
        primaryStageSplash.initStyle(StageStyle.UNDECORATED);
        primaryStageSplash.centerOnScreen(); // center on screen
        fxmlLoader.setControllerFactory(paramClass -> context.getBean(paramClass));

        try {
            BorderPane root = new BorderPane(fxmlLoader.load());
            scene = new Scene(root, 445, 299);
            //scene.getStylesheets().add(DeliveryManager.class.getResource("/css/main.css").toExternalForm());
            primaryStageSplash.setScene(scene);
            primaryStageSplash.show();

        } catch (IOException ex) {
            errorHandler.escalateCriticalFailure(ex);
        }
    }


private void showLoginScreen() {
        primaryStage = new Stage();

        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/fxml/LoginForm.fxml"), bundle);
        primaryStage.setMaxHeight(125.0);
        primaryStage.setMaxWidth(255.0);
        primaryStage.setResizable(false);
        primaryStage.initStyle(StageStyle.UNDECORATED);
        primaryStage.initModality(Modality.APPLICATION_MODAL);
        primaryStage.centerOnScreen(); // center on screen
        //Set the title from the resources.
        primaryStage.setTitle((String) bundle.getObject("UserInterface.LoginForm.Label.Title"));

        fxmlLoader.setControllerFactory(paramClass -> context.getBean(paramClass));

        if(operatingMode.equals(OperatingMode.REPRINT)) {
            primaryStage.getIcons().add(new Image("/images/reprint_utility_icon.png"));
        }
        else {
            primaryStage.getIcons().add(new Image("/images/delivery_manager_icon.png"));
        }

        try {
            BorderPane root = new BorderPane(fxmlLoader.load());
            scene = new Scene(root, 270, 180);
            root.setOnMousePressed(new MousePressHandler());
            root.setOnMouseDragged(new MouseDragHandler());
            scene.getStylesheets().add(DeliveryManager.class.getResource("/css/main.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();

        } catch (IOException ex) {
            errorHandler.escalateCriticalFailure(ex);
        }
    }



@Override
    public void stop(){
        Event event = new Event(null, primaryStage,null);
        ((Node)(event.getSource())).getScene().getWindow().hide();

        event = new Event(null, primaryStageSplash,null);
        ((Node)(event.getSource())).getScene().getWindow().hide();
        //primaryStageSplash.close();
    }

`

最佳答案

实际上,如果您只想在主 Stage 关闭时关闭弹出 Stage,则不必监听任何事件,可以设置主阶段为 owner您的启动画面。

A stage can optionally have an owner Window. When a window is a stage's owner, it is said to be the parent of that stage. When a parent window is closed, all its descendant windows are closed. The same chained behavior applied for a parent window that is iconified. A stage will always be on top of its parent window. The owner must be initialized before the stage is made visible.

primaryStageSplash.initOwner(primaryStage);

唯一的缺点是,子窗口将始终位于其父窗口之上。如果对你来说有问题的话,你仍然可以听听onCloseRequestProperty的变化。你的初级阶段。

Called when there is an external request to close this Window.

primaryStage.setOnCloseRequest((e) -> {
    primaryStageSplash.close();
});

我知道您说过它不起作用,但它应该起作用,因为它是检测外部关闭请求的“官方”方法。

关于java - 当我通过单击窗口的 [X] 红色按钮关闭屏幕时会调用哪个事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38388688/

相关文章:

java - AWS S3 Java SDK 不将文件复制到文件夹

java - 正确使用 GridBagLayout 设计面板

Java:检查字符串是否包含多个字符

wpf - 在主窗口的边界处剪辑 wpf 弹出窗口

java - 如何执行包含双引号的 PowerShell 命令

java - 使用FileReader读取文件

java - Canvas 画不出流畅的线条

JavaFX:ExecutorService.awaitTermination 不通过绑定(bind)属性更新 UI

java.io.FileNotFoundException : Acess Denied using jexcel

swift - 如何向停靠栏中的应用程序添加菜单?