java 如何在 JavaFX 应用程序中获取主要阶段?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15805881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:55:53  来源:igfitidea点击:

How can I obtain the primary Stage in a JavaFX application?

javajavafx

提问by Sergio

Is it possible to get a reference to the primary Stage in a running JavaFX application ?.

是否可以在运行的 JavaFX 应用程序中获得对主要阶段的引用?。

The context of this question is that I would like to write a library that manipulates a JavaFX interface from another language (Prolog). In order to do this, my library requires access to the primary Stage. The objective is that the programmer of the JavaFX application does not have to explicit store a reference to the Stage object in the start method, so it should be transparent for the user interface designer (this is a related questionin case more details are needed).

这个问题的上下文是我想编写一个库来操作来自另一种语言(Prolog)的 JavaFX 接口。为此,我的图书馆需要访问初级阶段。目标是 JavaFX 应用程序的程序员不必在 start 方法中显式存储对 Stage 对象的引用,因此它应该对用户界面设计人员透明(这是一个相关问题,以防需要更多详细信息) .

Part of this problem is getting a reference to the primary Stage object of the original JavaFX application ,so I was wondering if something like a static method somewhere could give me access to that.

这个问题的一部分是获取对原始 JavaFX 应用程序的主要 Stage 对象的引用,所以我想知道是否像某个地方的静态方法可以让我访问它。

回答by Vladimir Z.

Not sure of the right decision, but it works for my case.

不确定正确的决定,但它适用于我的情况。

Create static field in main class with getter and setter:

使用 getter 和 setter 在主类中创建静态字段:

public class MyApp extends Application {

    private static Stage pStage;

    @Override
    public void start(Stage primaryStage) {
        setPrimaryStage(primaryStage);

        pStage = primaryStage;
        ...
    }

    public static Stage getPrimaryStage() {
        return pStage;
    }

    private void setPrimaryStage(Stage pStage) {
        MyApp.pStage = pStage;
    }
}

Next, in the necessary place calling getter. For example:

接下来,在必要的地方调用 getter。例如:

stageSecond.initOwner(MyApp.getPrimaryStage());

回答by Vladimir Z.

I was just researching this issue. There does not seem to be any built-in method to get the primary stage. You can use node.getScene().getWindow() from a node within the main window but not within other windows. Explicitly storing the primaryStage reference is apparently the only reliable way to do this.

我只是在研究这个问题。似乎没有任何内置方法来获得初级阶段。您可以从主窗口内的节点使用 node.getScene().getWindow() ,但不能在其他窗口内使用。显式存储 primaryStage 引用显然是唯一可靠的方法。

回答by Nicolas Mommaerts

Since you receive the primary stage in the Application#start(Stage primaryStage) method you could keep it around.

由于您在 Application#start(Stage primaryStage) 方法中收到初级阶段,因此您可以保留它。

回答by Thiago Sarkis

Just to complement the VladmirZ Answer. Follow the first part. Then. For hide or close the primaryStage on Action Button.

只是为了补充 VladmirZ 的答案。按照第一部分。然后。用于隐藏或关闭操作按钮上的 primaryStage。

I do this. Example

我这样做。例子

void ActionButton(ActionEvent event) throws Exception {


    Stage StageTest = TabelaApp.getTabelaStage(); // call the getter. In my case TabelaApp.getTabelaStage
    StageTest.hide(); //Or Close

    // This Call the Stage you want
    Parent root = FXMLLoader.load(getClass().getResource("/tabela2/FrmTabela2.fxml"));
    Scene scene = new Scene(root);
    Stage stage = new Stage();
    stage.setScene(scene);
    stage.show();


}