摇摆到JavaFX2.0 [英] Swing to JavaFX2.0

查看:61
本文介绍了摇摆到JavaFX2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Swing转到JavaFX2.0.我有一个带有各种JFrame窗口的内部服务,用于显示/更改内部参数.我有一个带有按钮的菜单.每个按钮都会像这样启动一个JFrame窗口:

I’m moving from Swing to JavaFX2.0. I have an internal service with various JFrame windows to display/alter internal parameters. I have a menu with buttons. Each button kicks off one of the JFrame windows as such:

new ASvr().setVisible(true);

工作正常.现在转到JavaFX2.0:每个窗口"都是一个独立的FXML应用程序.当服务使用RPC与服务进行远程通信时,独立的FXML应用程序将作为独立的应用程序运行.这是内部版本,其中窗口"是同一JVM的一部分.

Works fine. Now going to JavaFX2.0: Each "window" is an independent FXML application. The independent FXML applications run as standalone applications when the service is remote using RPC to communicate with the service. This is the internal version where the "windows" are part of the same JVM.

菜单以application.launch()开头. JVM中只能有一个launch(),因此我不能对独立的FXML应用程序使用launch().经过反复尝试(大多数都是错误),我想到的是:

The Menu starts with application.launch(). There can only be one launch() in the JVM so I can’t use launch() for the independent FXML applications. After much trial and error (mostly error) what I’ve come up with is:

new ASvr().start(new Stage());

我认为很好.由于我是JavaFX的新手,所以我可能会缺少一些东西,这些东西会破坏该解决方案.我自己而不是从launch()调用start()可能会有讨厌的副作用.我无法使用新的ProcessBuilder并将每个应用程序放入新的JVM中,因为我需要直接访问内部服务中的引用.

Works fine, I think. Since I’m new to JavaFX I could be missing something which will ruin this solution. Calling start() myself, not from a launch(), may have nasty side effects. I can’t use a new ProcessBuilder and put each application inside a new JVM since I need direct access to references within the internal service.

我的问题:是否有一种首选的方法来在JVM中运行几个独立的(需要独立运行而无需更改)FXML应用程序场景?您发现我的工作有任何潜在的问题吗?

My questions: Is there a preferred way to have several independent (need to run as standalone without alteration) FXML application scenes running within a JVM? Do you see any potential problems with what I did?

推荐答案

我不建议自己致电start(...). launch(...)方法执行许多复杂的内务处理,例如启动FX工具包和FX Application线程,实例化应用程序类,然后在FX Application线程上调用start(...).

I would not recommend calling start(...) yourself. The launch(...) method does a lot of complex housekeeping, such as starting the FX toolkit and the FX Application Thread, instantiating the application class, and then invoking start(...) on the FX Application thread.

您基本上应该将start(...)视为JavaFX应用程序的main(...)方法的等效项,并且将Application子类视为主类",而不是JFrame子类. (实际上,在Java 8中,如果您的类是Application的子类,则可以直接由JVM启动它,甚至没有main(...)方法.)

You should basically think of start(...) as the equivalent of the main(...) method for a JavaFX application, and the Application subclass as the "main-class", not the JFrame subclass. (Indeed, in Java 8, if your class is a subclass of Application it can be launched directly by the JVM without even having a main(...) method.)

一旦创建了单个start(...)方法(同样,可以将其替换为来自常规" Java应用程序的单个main(...)入口点),则可以显示FXML文件的内容用成语

Once you have created your single start(...) method (again, just think of it as the replacement for your single main(...) entry point from a "regular" Java application), then you can show the contents of an FXML file with the idiom

Stage stage = new Stage();
stage.setScene(new Scene(FXMLLoader.load(fxmlResource));
stage.show();

(当然,这有所不同,特别是如果您需要访问控制器,但您有所了解的话.)

(Of course, there are variations on this, especially if you need access to the controller, but you get the idea.)

如果想要创建Stage的子类,可以采用创建JFrame子类的旧样式,然后让Stage子类的构造函数加载其FXML并设置自己的场景.然后,您可以使用与Swing相同的习惯用法:

You could if you wanted create subclasses of Stage, in the old swing style of creating JFrame subclasses, and let the constructor of the Stage subclass load its FXML and set its own scene. Then you could use essentially the same idiom as you used in Swing:

new MyStageSubclass().show();

再次注意JFrame子类的类比是Stage子类,而不是Application子类.

Notice again how the analogue of a JFrame subclass is a Stage subclass, not an Application subclass.

这篇关于摇摆到JavaFX2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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