JavaFX - 在主舞台关闭时关闭任何其他舞台,但允许它们隐藏在主舞台后面 [英] JavaFX - Make any other stages close when the main stage is closed, yet allow them to be hidden behind the main stage

查看:280
本文介绍了JavaFX - 在主舞台关闭时关闭任何其他舞台,但允许它们隐藏在主舞台后面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们说我正在创建第二个窗口的代码如下:

Let us say I have the following code for a second window I'm creating:

    stage.initModality(Modality.NONE);
    stage.initOwner(coreController.getStage());

如果我这样做,它将在我的主窗口关闭时关闭。

If I do this, it will close when my main window closes.

如果我注释掉 stage.initOwner(coreController.getStage()); ,那么它将能够隐藏在主面板后面,但是它不会关闭。

If I comment out stage.initOwner(coreController.getStage());, then it will be able to be hidden behind the main panel, but it will not close.

当我杀死主舞台时有没有办法让它关闭... 没有编写代码跟踪创建的每个新窗口?到目前为止,似乎我唯一的选择是在列表中注册每个新窗口,当主窗口发出关闭事件时,我也会为所有其他窗口启动它。有没有办法做到这一点,我不必进入并将所有内容连接在一起?

Is there any way to get it to close when I kill the main stage... without writing code to track every single new window created? So far it seems my only option is to register each new window in a list, and when the main window has a 'close event' fired, I also fire this for all the other windows. Is there a way to do it without me having to go in and wire everything together like that?

编辑:我使用了Modality.NONE所以窗口不会阻止任何事件,但我不知道这是否与问题相关。

I used Modality.NONE so that no events will be blocked by the window, I don't know if this is relevant to the question however.

编辑2: I我希望在退出主窗口时关闭所有窗口(如果我能拦截关闭事件并进行清理,那将是一个奖励。)

Edit 2: I would like to have all the windows close upon exiting the main window (it would be a bonus if I could intercept the close events and do cleanup too).

推荐答案

如果您只想关闭所有子窗口,但由于某些其他原因(例如您打开兄弟窗口等等)保持应用程序运行,那么我认为您必须自己管理你在问题中建议(保留一个窗口列表并单独调用 hide()等)。

If you just want to close all the child windows, but keep the application running for some other reason (e.g. you have sibling windows open, etc...) then I think you have to manage that yourself as you suggested in the question (keep a list of windows and call hide() on them individually, etc).

如果您实际上计划退出该应用程序,那么您可以这样做:

If you are actually planning to exit the application, then you can just do:

mainWindow.setOnHidden(e -> Platform.exit());

这会导致JavaFX系统正常退出(这当然会导致其他每个窗口都出现关)。

That will cause the JavaFX system to gracefully exit (which of course will cause each of the other windows to close).

如果需要清理资源,可以在应用程序子类中覆盖 stop()并清理该方法中的资源。通过 Platform.exit()关闭应用程序时,将为您调用 stop()(但请注意,它不会如果您执行 System.exit(...),则调用。

If you need to clean up resources, you can override stop() in your application subclass and clean up the resources in that method. stop() will be called for you when closing the application via Platform.exit() (but note it will not be called if you do a System.exit(...)).

如果您需要清理基于每个窗口的资源,然后您可以在每个窗口中注册 onHidden()处理程序。请注意,如果随后 show()隐藏了相同的窗口实例,则表示您可能需要恢复这些资源。这个用例非常不典型。

If you need to clean up resources on a per-window basis, then you can register onHidden() handlers with each window to do so. Note that if you subsequently show() the same window instance after it has been hidden, this means you may need to reinstate those resources. This use case is pretty untypical though.

这是一个准系统示例:

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class ExitOnMainWindowClose extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button newWindowButton = new Button("New Window");
        newWindowButton.setOnAction(e -> showNewWindow());
        primaryStage.setScene(new Scene(new StackPane(newWindowButton), 120, 75));
        primaryStage.setOnHidden(e -> Platform.exit());
        primaryStage.show();
    }

    @Override
    public void stop() {
        System.out.println("Application-level cleanup...");
    }

    private void showNewWindow() {
        Stage stage = new Stage();
        stage.setScene(new Scene(new StackPane(new Label("New Window")), 180, 60));
        stage.setOnHidden(e -> System.out.println("Window-level cleanup..."));
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

这篇关于JavaFX - 在主舞台关闭时关闭任何其他舞台,但允许它们隐藏在主舞台后面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆