我正在尝试将程序从 java 8 迁移到 java 9。
在我的程序中,我找到了以下代码。
//Run JavaFX Application Thread/Toolkit
PlatformImpl.startup(() -> {
});
它尝试启动 JavaFX Toolkit
不幸的是,java 9 不再支持 PlatformImpl.startup。
有什么替代品吗?
如何启动 JavaFX 工具包?
谢谢
最佳答案
startup()
方法是从非公共(public)类 PlatformImpl
提升到公共(public) API Platform
类的方法之一在 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 theApplication
class to launch the application, the FX runtime is initialized automatically by the launcher before theApplication
class is loaded.
- For Swing applications that use
JFXPanel
to display FX content, the FX runtime is initialized when the firstJFXPanel
instance is constructed.- For SWT application that use
FXCanvas
to display FX content, the FX runtime is initialized when the firstFXCanvas
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 应用程序。
关于javafx - 在 Java9 中运行 JavaFX 应用程序线程/工具包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48811237/