javafx - 在 Java9 中运行 JavaFX 应用程序线程/工具包

标签 javafx migration java-9

我正在尝试将程序从 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 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 应用程序。

关于javafx - 在 Java9 中运行 JavaFX 应用程序线程/工具包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48811237/

相关文章:

java - 如何在 Intellij 的同一个 jar 中导出 JavaFX 应用程序和依赖项?

java - 为什么在尝试将数据放入我的 JavaFX 散点图中时出现 IndexOutOfBoundsException?

javafx - TornadoFX 中的连续动画?

python - 如何从 Django 迁移中调用基于实例的函数并访问实例变量

java - 为什么未模块的 JAR 可以访问模块 JAR 的非公开包?

java - Java 和 JavaFX 版本必须匹配吗?

mysql - RAILS 将触发器添加到迁移中

Spring 3.2 配置 tx :annotation-driven error in eclipse

java - Eclipse 不显示 Java 9 选项

Java 9 整数到字符串转换