运行 Java9 中的 JavaFX 应用程序 Thread/Toolkit

Run JavaFX Application Thread/Toolkit in Java9

我正在尝试将程序从 java 8 迁移到 java 9。

在我的程序中我发现了下面的代码。

//Run JavaFX Application Thread/Toolkit
    PlatformImpl.startup(() -> {
    });

它尝试启动 JavaFX 工具包

很遗憾,PlatformImpl.startup 在 java 9 中不再受支持。

有什么替代品?

如何启动 JavaFX 工具包?

谢谢

startup()方法是从non-publicclassPlatformImpl提升到publicAPI的方法之一Platform class 在 Java 9 版本中。现在已在 API documentation.

中完整记录

因此 Java 9 中的等效调用是

Platform.startup(() -> { });

请注意,此方法的用例相当少见,API 文档竭尽全力强调这一点:

In general it is not necessary to explicitly call this method, since it is invoked as a consequence of how most JavaFX applications are built.

...

As noted, it is normally the case that the JavaFX Application Thread is started automatically. It is important that this method only be called when the JavaFX runtime has not yet been initialized. Situations where the JavaFX runtime is started automatically include:

For standard JavaFX applications that extend Application, and use either the Java launcher or one of the launch methods in the Application class to launch the application, the FX runtime is initialized automatically by the launcher before the Application class is loaded.

  • For Swing applications that use JFXPanel to display FX content, the FX runtime is initialized when the first JFXPanel instance is constructed.
  • For SWT application that use FXCanvas to display FX content, the FX runtime is initialized when the first FXCanvas instance is constructed. When an application does not follow any of these common approaches, then it becomes the responsibility of the developer to manually start the JavaFX runtime by calling this startup method.

Calling this method when the JavaFX runtime is already running will result in an IllegalStateException being thrown - it is only valid to request that the JavaFX runtime be started once.

因此,在进行更改的过程中,您需要将应用程序更新为 Java 9 兼容,您可能需要仔细考虑是否需要致电 startup()全部;无论如何,也许有一种更标准和更强大的方法来启动您的 JavaFX 应用程序。