首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >JavaFX可以在非模块化环境中实际使用吗?

JavaFX可以在非模块化环境中实际使用吗?
EN

Stack Overflow用户
提问于 2021-05-24 09:34:05
回答 1查看 138关注 0票数 1

我尝试通过以下地址找到的说明来使用JavaFX:

"JavaFX -> IntelliJ“下的https://openjfx.io/openjfx-docs/和"Non-modular with maven.”

在完成步骤1和2(安装和验证)之后,我尝试从IntelliJ运行一个非常简单的程序,而不是通过Maven插件。

TraderWindow.java

代码语言:javascript
运行
复制
public class TraderWindow extends Application {

    @SneakyThrows
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    @SneakyThrows
    public void start(Stage primaryStage) {
        Scene scene = FXMLLoader.load(getClass().getResource("/TraderWindow.fxml"));
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

TraderWindow.fxml

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1">
  <columnConstraints>
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
  </columnConstraints>
  <rowConstraints>
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
  </rowConstraints>
   <children>
      <Button mnemonicParsing="false" text="Button" />
   </children>
</GridPane>

然而,当我尝试运行它时,我得到了:

代码语言:javascript
运行
复制
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module @0x6adc19e3) cannot access class com.sun.javafx.util.Utils (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.util to unnamed module @0x6adc19e3
    at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
    at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2138)
    at com.trader.gui.TraderWindow.start(TraderWindow.java:19)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:474)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:447)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:446)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    ... 1 more
Exception running application com.trader.gui.TraderWindow

Process finished with exit code 1

相关部分:

代码语言:javascript
运行
复制
Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module @0x6adc19e3) cannot access class com.sun.javafx.util.Utils (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.util to unnamed module @0x6adc19e3

我想这是可以理解的,因为JafaFx是模块化的,而我的应用程序不是。为了绕过这个问题,我将这个添加到了VM路径中:

--add-opens javafx.graphics/com.sun.javafx.util=ALL-UNNAMED

我又试了一次,这次得到了:

代码语言:javascript
运行
复制
Caused by: java.lang.IllegalAccessError: class javafx.fxml.JavaFXBuilderFactory (in unnamed module @0x4e342850) cannot access class com.sun.javafx.reflect.ConstructorUtil (in module javafx.base) because module javafx.base does not export com.sun.javafx.reflect to unnamed module @0x4e342850

所以我添加了另一个--add-open指令...然后,在放弃之前,我又重复了3次这些步骤。

有没有其他方法可以让JavaFX与Java 11+一起工作,而不是模块化应用程序或为每个非法访问操作费力地添加--add-opens指令?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-24 16:01:06

如果你想彻底摆脱所有的模块系统问题,你可以在你的主类中添加如下一行

代码语言:javascript
运行
复制
class MyAppLauncher {public static void main(String[] args) {MyApp.main(args);}}

然后通过这个启动器启动应用程序。如果这样做,您可以将所有内容放在类路径中,并删除all -add-open。(实际上你必须这样做。)唯一的缺点是,从JavaFX 16开始,你会收到一条讨厌的警告消息,上面写着Unsupported JavaFX configuration,但你可以忽略它。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67665764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档