JavaFX是如何准确启动的 [英] How does JavaFX start exactly
问题描述
我刚刚编写了一个 JavaFX
应用程序,并意识到我并不真正理解它是如何实际启动的。
I've just written a JavaFX
application and realized I don't really understand how it actually starts up.
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = new FXMLLoader(this.getClass().getResource("view.fxml")).load();
primaryStage.setTitle("Dice Roller");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
这是Intellij给我的模板。我的入口点将是主要的,其中唯一运行的是 launch(args)
。我试过在Application类中挖掘,但没有找到任何指向运行start方法的东西。这甚至是如何推出的?由于 JavaFX
拥有它自己的线程,我假设在启动方法中创建了一个线程,并且主线程在启动之前不会返回,直到你调用 Platform.exit
或者只关闭所有窗口。这对我来说现在感觉有点太抽象了。有人可以向我解释它是如何组合在一起的吗?
This is the template given to me in Intellij. My entry point would be main where the only thing that gets run is launch(args)
. I've tried digging in the Application class but didn't find anything that would point towards running the start method. How is this even launched? Since JavaFX
has it's own thread I'm assuming a thread gets created in the launch method and the main thread doesn't return from launch until you call Platform.exit
or just close all windows. This all feels just a bit too abstract to me right now. Can someone explain to me how it all fits together?
推荐答案
以下是您的主要应用程序类必须是什么样子。我把它命名为HelloApp,因为我需要一个名字。我还用 Application.launch
改变了启动
,它是相同的但不那么令人困惑(见下面的解释)。
Below is what your main application class must look like. I named it "HelloApp" because I needed a name. I also changed launch
with Application.launch
, it's the same but less confusing (see explanations below).
但请注意(感谢@Cypher在评论中提出这一点) java
不需要好的旧 public static void main(String [] args)
运行JavaFX应用程序的方法。
如果省略下面的main
方法,编译它,然后用 java HelloApp
运行它,它会起作用,可能会稍微有些混乱:) >
您仍然可以在没有 main
的情况下处理命令行参数,因为它们被传递给参数对象,您可以从中获取应用程序
,带 getParameters()
。
Please note however (thanks @Cypher in comments for bringing this up) that java
does not need the good old public static void main(String[] args)
method to run a JavaFX application.
If you omit the main
method from below, compile it, and run it with java HelloApp
, it will work, and might be slightly less confusing :)
You can still handle command line arguments without main
, as they are passed to the parameters object, which you can obtain from within your Application
with getParameters()
.
我的IDE也允许运行一个申请
没有主
,显然大部分都是。
My IDE also allows to run an Application
without main
, and apparently most do.
话虽如此,让我们看看你的情况会发生什么:
That being said, let's see what happens here in your case:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class HelloApp extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = new FXMLLoader(this.getClass().getResource("view.fxml")).load();
primaryStage.setTitle("Dice Roller");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
-
HelloApp.main()
调用launch()
-
launch
是您扩展的Application
类中的静态方法,因此可以使用launch()
直接但在调用静态方法时,建议使用类名,所以我用Application.launch
替换上面的内容。如果你不这样做,一些IDE和短信警告你 java静态方法应该以静态方式访问。 - 在<$ c内$ c>启动方法,JavaFX运行时(在JavaFX下面)确定哪个类是调用者(通过使用
Thread.currentThread()。getStackTrace()
,但这只是一个有趣的实现细节)。该调用者类应该是javafx.application.Application
- 在没有main的情况下运行
,上面是无关紧要的,JavaFX
在这里开始。 - - JavaFX创建一个应用程序线程,用于运行应用程序启动方法,处理输入事件和运行动画时间轴。
- JavaFX构造指定Application类的实例
- JavaFX调用
init()
(可以覆盖) - JavaFX从JavaFX应用程序线程调用
start()
(您必须自己实现) HelloApp.main()
invokeslaunch()
launch
is a static method inApplication
class that you extends, so you can uselaunch()
directly but when calling static methods it's recommended to use the class name, so I replaced above withApplication.launch
. Some IDEs and linters warn you with "java static method should be accessed in a static way" if you don't do that.- Within the
launch
method, the JavaFX runtime (below "JavaFX") figures out which class is the caller (by usingThread.currentThread().getStackTrace()
, but that's just an interesting implementation detail). That caller class is expected to be ajavafx.application.Application
-- When running withoutmain
, the above is irrelevant, and JavaFX starts about here. -- - JavaFX creates an application thread for running the application start method, processing input events, and running animation timelines.
- JavaFX constructs an instance of the specified Application class
- JavaFX calls
init()
(which you can override) - JavaFX calls
start()
(which you must implement yourself), from the "JavaFX Application Thread"
生命周期的其余部分在应用程序javadoc - 后续步骤正在等待应用程序完成(应用程序调用 Platform.exit( )
,或者最后一个窗口已经关闭且 implicitExit
为真),然后JavaFX调用 stop()
(你可以覆盖)。
The rest of the lifecycle is described in Application javadoc -- subsequent steps are waiting for application to finish (either app calls Platform.exit()
, or last window has been closed and implicitExit
is true), then JavaFX calls stop()
(which you can override).
这篇关于JavaFX是如何准确启动的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!