如何在JavaFX应用程序中获取主阶段? [英] How can I obtain the primary Stage in a JavaFX application?

查看:115
本文介绍了如何在JavaFX应用程序中获取主阶段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在正在运行的JavaFX应用程序中获取对主舞台的引用?。

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

这个问题的上下文是我想写一个库从另一种语言(Prolog)操纵JavaFX接口。
为了做到这一点,我的库需要访问主要阶段。
目标是JavaFX应用程序的程序员不必在start方法中显式存储对Stage对象的引用,因此它对于用户界面设计者应该是透明的(这是相关问题如果有更多细节,需要)。

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 question in case more details are needed).

这个问题的一部分是获取对原始JavaFX应用程序的主要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.

推荐答案

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

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

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

Create static field in main class with getter and 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;
    }
}

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

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

stageSecond.initOwner(MyApp.getPrimaryStage());

这篇关于如何在JavaFX应用程序中获取主阶段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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