尝试使用FXML启动GUI时出现“应用程序启动方法异常”

我正在做一个大学项目,所以我必须创建一个GUI。我正在尝试学习FXML,但是在尝试启动GUI时出现以下错误:

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:567)
    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:567)
    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:835)
Caused by: java.lang.IllegalaccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module @0x7e12402b) 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 @0x7e12402b
    at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
    at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
    at gui.Main.start(Main.java:17)
    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:389)
    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 gui.Main

Mainmenu.fxml:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<VBox prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml"
      fx:controller="gui.MainmenuController">

    <Label text="I love bacon"/>
    <Button text="Submit"/>

</VBox>

MainmenuController.java:

package gui;

public class MainmenuController {

    public MainmenuController() {
    }
}

Main.java:

package gui;

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 {

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = FXMLLoader.load(getclass().getResource("Mainmenu.fxml"));
        primaryStage.setTitle("Netflix Statistix by S. Jaspers,I. Moerenhout & Z. Usmaeva");
        primaryStage.setScene(new Scene(root,400,600));
        primaryStage.show();
    }
}

项目结构:

尝试使用FXML启动GUI时出现“应用程序启动方法异常”

我对到底出什么问题一无所知。我在Mainmenu.fxml文件中指的是正确的文件名,而我的Main类则应按原样引用Mainmenu.fxml。我也尝试引用“ gui.Mainmenu.fxml”和“ /gui/Mainmenu.fxml”,但似乎都无法正常工作。我查看了错误消息,发现了很多StackOverflow问题,但是这些解决方案似乎都不适合我的情况,也没有帮助。

wxcool87 回答:尝试使用FXML启动GUI时出现“应用程序启动方法异常”

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2819004.html

大家都在问