Application类
Application 应用程序
JavaFX 应用程序扩展的应用程序类。
package javafx.application;
public abstract class Application { }Application 生命周期
Application 是 JavaFX 应用程序的入口类。每当应用程序启动时,JavaFX 运行时都会依次执行以下操作:
如果 JavaFX 运行时尚未启动,则启动 JavaFX 运行时(见:
Platform.startup(Runnable))。构建指定应用程序类的实例
调用
init()方法:应用程序初始化方法,默认什么都不干。- 该方法在应用程序类加载并构建后立即调用,会启用一个新线程来调用。
- JavaFX 应用程序线程不会调用此方法,故不能在这里新建 Stage、Scene,可在此方法中构建其他 JavaFX 对象。
调用
start(Stage primaryStage)方法:所有 JavaFX 应用程序的主要入口点。抽象方法,所有 Application 实现类必需重写此方法。
此方法在 JavaFX 应用程序线程上调用。
start 方法在 init 方法返回后调用,在系统准备好让应用程序开始运行后调用。
等待应用程序结束,当出现以下任一情况时即为结束:
调用
Platform.exit()。(显式关闭)最后一个窗口已关闭,且
Platform上的implicitExit属性为true(默认)。调用
System.exit(int)也可以实现退出应用程序的效果,但不会执行stop()方法。
调用
stop()方法:默认什么也不干。
- 执行
System.exit(int)退出或stop()执行后,都不能再使用 JavaFX。 - JavaFX 类必须从模块路径上一组名为
javafx.*的模块中加载。不支持从 classpath 加载 JavaFX 类。更多信息请参见Platform.startup(Runnable)。
将应用程序作为模块部署
If the Application subclass is in a named module then that class must be accessible to the javafx.graphics module. Otherwise, an exception will be thrown when the application is launched. This means that in addition to the class itself being declared public, the module must export (or open) the containing package to at least the javafx.graphics module.
如果
Application子类位于已命名模块中,则该类必须能被javafx.graphics模块访问。否则,应用程序启动时将出现异常。这意味着,除了Application类本身要声明为公有外,模块还必须 export (或 open )至少是javafx.graphics模块的包含包。
For example, if com.foo.MyApplication is in the foo.app module, the module-info.java might look like this:
例如,如果
com.foo.MyApplication在foo.app模块中,那么module-info.java文件可能会是这样:
module foo.app {
exports com.foo to javafx.graphics;
}- 必须直接或者间接地向
javafx.graphics模块暴露 Application 实现类所在的模块。
Threading
JavaFX creates an application thread for running the application start method, processing input events, and running animation timelines. Creation of JavaFX Scene and Stage objects as well as modification of scene graph operations to live objects (those objects already attached to a scene) must be done on the JavaFX application thread.
JavaFX 会创建一个应用程序线程,用于运行应用程序启动方法、处理输入事件和运行动画时间线。JavaFX Scene 和 Stage 对象的创建以及对实时对象(已连接到场景的对象)的场景图操作修改必须在 JavaFX 应用程序线程上完成。
The JavaFX Application Thread is created as part of the startup process for the JavaFX runtime.
JavaFX 应用程序线程是作为 JavaFX 运行时启动过程的一部分创建的。
The Java launcher loads and initializes the specified Application class on the JavaFX Application Thread. If there is no main method in the Application class, or if the main method calls Application.launch(), then an instance of the Application is then constructed on the JavaFX Application Thread.
Java 启动器会在 JavaFX 应用程序线程上加载并初始化指定的应用程序类。如果应用程序类中没有主方法,或者主方法调用了 Application.launch(),则会在 JavaFX 应用程序线程上构建应用程序实例。
The init method is called on the launcher thread, not on the JavaFX Application Thread.
init 方法在启动线程上调用,而不是在 JavaFX 应用程序线程上调用。
All the unhandled exceptions on the JavaFX application thread that occur during event dispatching, running animation timelines, or any other code, are forwarded to the thread's uncaught exception handler.
JavaFX 应用程序线程在事件分派、运行动画时间线或其他代码时出现的所有未处理异常都会被转发到线程的 UncaughtExceptionHandler 中。
- 调用
Thread.set...UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)设置线程的 UncaughtExceptionHandler。
Application 常用 API
Application 类提供的实例方法,被final修饰无法重写。
HostServices getHostServices();
Application.Parameters getParameters();