我尝试用一个TextField和Button创建一个屏幕,并接收“ Application start method中的异常”。第一次,我尝试从这些问题中解决问题,但不起作用:
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>
控制器类:
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>
答案 0 :(得分:1)
问题与您提供给FXMLLoader
的路径有关。
如果您的FXML文件位于src/main/resources/userInterface/CreateProjectScreen.fxml
中,那么您的FXMLLoader
应按以下方式调用:
Parent parent = FXMLLoader.load(getClass().getResource("/userInterface/CreateProjectScreen.fxml"));
前斜杠指的是类路径的根(即src/main/resources
)。紧随其后的路径是FXML文件的相对路径。
您还收到模块访问错误。您需要导出错误消息中列出的模块,否则JavaFX将无法访问它。
答案 1 :(得分:0)
就我而言,我只是将 Main.java 文件移动到 src/Main/java 中。这对我有用
答案 2 :(得分:-2)
这不是专家的问题。
Java 9引入了模块化,因此您必须编写名为
的模块描述符。module-info.java
位于包javafx.graphics的根。
然后添加:
module javafx.graphics {
exports com.sun.javafx.util;
}
(如您所说的stacktrace)。