本文介绍了应用程序启动方法中的异常.JavaFx 11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用一​​个TextField和Button创建一个屏幕,并接收"Application start method中的异常".第一次,我尝试从这些问题中解决问题,但不起作用:

I try to create a screen with one TextField and Button and receve "Exception in Application start method". Firt time I try solution from this questions and dont work:

应用程序启动方法异常

应用程序启动方法javafx gui中的异常

> JavaFX位置为必需的."即使在同一包中

应用程序启动方法错误中的异常.其他解决方案不起作用

我使用Java 11,javaFx 11.对于javaFx,我使用maven.

I use Java 11, javaFx 11.For javaFx I use 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>

推荐答案

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

The issue is to do with the path that you are providing to your FXMLLoader.If your FXML file is in src/main/resources/userInterface/CreateProjectScreen.fxml, then your FXMLLoader should be called as follows:

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

前斜杠指的是类路径的根(即 src/main/resources ).紧随其后的路径是FXML文件的相对路径.

The leading slash refers to the root of the classpath (i.e. src/main/resources). The path following that is the relative path to the FXML file.

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

You are also getting a module access error. You need to export the module listed in the error message, otherwise JavaFX will not be able to access it.

这篇关于应用程序启动方法中的异常.JavaFx 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 19:25
查看更多