gpt4 book ai didi

java - 应用程序启动方法中出现异常。 JavaFX 11

转载 作者:行者123 更新时间:2023-11-30 01:48:02 32 4
gpt4 key购买 nike

我尝试创建一个带有一个文本字段和按钮的屏幕,并收到“应用程序启动方法中的异常”。我第一次尝试解决这个问题但不起作用:

Exception in Application start method

Exception in Application start method javafx gui

JavaFX "Location is required." even though it is in the same package

Exception in Application start method error. Other solutions not working

我使用 Java 11、javaFx 11。对于 javaFx,我使用 maven。

主类是:

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

public class StartClass extends Application{
public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage stage) throws Exception {
Parent parent = FXMLLoader.load(getClass().getResource("../userInterface/CreateProjectScreen.fxml"));
stage.setTitle("IEPCP");
stage.setScene(new Scene(parent ));
stage.show();
}

}

FXML 文件是:

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

<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.ui.CreateProjectController">
<children>
<AnchorPane prefHeight="402.0" prefWidth="601.0">
<children>
<Button fx:id="sendButton" layoutX="249.0" layoutY="310.0" mnemonicParsing="false" onAction="#sendButtonClicked" text="Button" />
<TextField fx:id="directory" layoutX="66.0" layoutY="85.0" prefHeight="25.0" prefWidth="470.0" />
</children>
</AnchorPane>
</children>
</AnchorPane>

Controller 类:

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

public class CreateProjectController {

@FXML
private TextField directory;

@FXML
private Button sendButton;

public TextField getDirectory() {
return directory;
}

public void setDirectory(TextField directory) {
this.directory = directory;
}

public void sendButtonClicked(ActionEvent actionEvent){
System.out.println("You write: " + getDirectory().getText());
}
}

这是收到的错误:

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:566)
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:566)
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:834)
Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module @0x3e122bd1) 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 @0x3e122bd1
at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
at primary.StartClass.start(StartClass.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(Native Method)
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 primary.StartClass

Maven 依赖项:

<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>11</version>
</dependency>
</dependencies>

最佳答案

问题与您提供给 FXMLLoader 的路径有关。如果您的 FXML 文件位于 src/main/resources/userInterface/CreateProjectScreen.fxml 中,则您的 FXMLLoader 应按如下方式调用:

Parent parent =     FXMLLoader.load(getClass().getResource("/userInterface/CreateProjectScreen.fxml"));

前导斜杠指的是类路径的根(即src/main/resources)。接下来的路径是 FXML 文件的相对路径。

您还收到模块访问错误。您需要导出错误消息中列出的模块,否则JavaFX将无法访问它。

关于java - 应用程序启动方法中出现异常。 JavaFX 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57221488/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com