为什么父根= FXMLLoader无法正常工作? [英] Why Parent root =FXMLLoader is not working properly?

查看:80
本文介绍了为什么父根= FXMLLoader无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在JavaFX中构建应用程序,但是我正面临这个问题. 下面的代码生成error.I错误显示自创建项目以来. 我从"https://gluonhq.com/products/javafx/"下载了JavaFX 14.并包含lib目录中存在的所有jar文件.

I am trying to build an app in JavaFX but i am facing the issue. Below code generating error.I error are showing since i created the project. I downloaded the JavaFX 14 from "https://gluonhq.com/products/javafx/" and include all jar files present in lib directory.


import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }


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

但是此代码可以正常工作

But This code works fine

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        //Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        StackPane root=new StackPane();
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }


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

出现此错误:

"C:\Program Files\Java\jdk-14.0.1\bin\java.exe" --add-modules javafx.base,javafx.graphics --add-reads javafx.base=ALL-UNNAMED --add-reads javafx.graphics=ALL-UNNAMED "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.1.3\lib\idea_rt.jar=60984:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.1.3\bin" -Dfile.encoding=UTF-8 -classpath E:\Shapes\out\production\Shapes;E:\Software\javafx-sdk-14.0.2.1\lib\javafx-swt.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.base.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.controls.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.fxml.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.graphics.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.media.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.swing.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.web.jar -p E:\Software\javafx-sdk-14.0.2.1\lib\javafx.base.jar;E:\Software\javafx-sdk-14.0.2.1\lib\javafx.graphics.jar sample.Main
Exception in Application start method
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 @0x66dac2b5) 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 @0x66dac2b5
    at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
    at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
    at sample.Main.start(Main.java:14)
    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:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    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 sample.Main

Process finished with exit code 1

推荐答案

基本错误是(为便于阅读而设置):

The fundamental error is (formatted for readability):

Caused by: java.lang.IllegalAccessError: 
    class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module @0x66dac2b5) 
    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 @0x66dac2b5

该错误告诉您,当javafx.graphics模块位于模块路径上时,javafx.fxml模块已经结束在类路径上.这种情况会阻止对javafx.graphics模块授予javafx.fxml模块的内部代码的特殊访问.

That error is telling you that the javafx.fxml module has ended up on the class-path while the javafx.graphics module is on the module-path. This scenario prevents the special access to internal code the javafx.graphics module grants to the javafx.fxml module.

解决方案是执行以下操作之一:

The solution to this is to do one of the following:

  1. --add-modules VM参数中包含javafx.fxml.
  2. 使您自己的代码模块化,添加必要的 requires exports opens 指令,然后使用--module启动您的应用程序.
  1. Include javafx.fxml in your --add-modules VM argument.
  2. Make your own code modular, add the necessary requires, exports, and opens directives, and launch your application with --module.

如果您对上述错误的产生方式感到好奇,那么我对另一个问题的回答会更详细.请注意,它专注于javafx.media模块,但是概念是相同的.

If you're curious about how the above error comes about then my answer to another question goes into more detail. Note it focuses on the javafx.media module but the concept is the same.

这篇关于为什么父根= FXMLLoader无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆