gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:16:06 25 4
gpt4 key购买 nike

如果我通过按右上角的 [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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com