java - 当我尝试加载 FXML 文件时,出现 java.io.IOException 错误

标签 java intellij-idea javafx fxml fxmlloader

我正在尝试加载使用场景生成器构建的 fxml 文件,但由于某种原因,我收到 java.io.IOException 错误。

这是我得到的错误:

/Library/Java/JavaVirtualMachines/jdk-12.jdk/Contents/Home/bin/java --module-path /Users/anish/codingprojects/javafx-sdk-11.0.2/lib --add-modules javafx.controls,javafx.fxml --add-modules javafx.base,javafx.graphics --add-reads javafx.base=ALL-UNNAMED --add-reads javafx.graphics=ALL-UNNAMED "-javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=53528:/Applications/IntelliJ IDEA CE.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Users/anish/IdeaProjects/FBLA/out/production/FBLA:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx-swt.jar:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx.base.jar:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx.controls.jar:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx.fxml.jar:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx.graphics.jar:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx.media.jar:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx.swing.jar:/Users/anish/codingprojects/javafx-sdk-11.0.2/lib/javafx.web.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: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: javafx.fxml.LoadException: 
/Users/anish/IdeaProjects/FBLA/out/production/FBLA/sample/mainpage.fxml:35

    at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2603)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
    at sample.Main.start(Main.java:13)
    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)
Caused by: java.lang.IllegalArgumentException: Can not set javax.swing.text.html.ListView field sample.MainPageController.hoursLog to javafx.scene.control.ListView
    at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at java.base/jdk.internal.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
    at java.base/java.lang.reflect.Field.set(Field.java:780)
    at javafx.fxml/javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1174)
    at javafx.fxml/javafx.fxml.FXMLLoader.access$1600(FXMLLoader.java:105)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:865)
    at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:759)
    at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
    ... 14 more
Exception running application sample.Main

这是我的主类代码:

package sample;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;

import java.io.IOException;

public class addHours {
    @FXML
    public void display()
    {
        try {
            Pane addHoursPane = FXMLLoader.load(getClass().getResource("addHours.fxml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这是我使用场景生成器构建的 FXML 文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.TextFlow?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="518.0" prefWidth="704.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.MainPageController">
    <children>
        <MenuBar AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
            <menus>
                <Menu mnemonicParsing="false" text="File">
                    <items>
                        <MenuItem mnemonicParsing="false" text="Close" />
                    </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Edit">
                    <items>
                        <MenuItem mnemonicParsing="false" text="Delete" />
                        <MenuItem mnemonicParsing="false" text="Unspecified Action" />
                    </items>
                </Menu>
            </menus>
        </MenuBar>
        <SplitPane fx:id="splitPane" dividerPositions="0.23062015503875968" orientation="VERTICAL" prefHeight="518.0" prefWidth="704.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="155.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="28.0">
            <items>
                <AnchorPane maxHeight="300.0" minHeight="100.0" prefHeight="100.0" prefWidth="160.0" />
                <AnchorPane prefHeight="100.0" prefWidth="160.0">
                    <children>
                        <ListView fx:id="hoursLog" layoutX="136.0" layoutY="65.0" prefHeight="393.0" prefWidth="527.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
                    </children>
                </AnchorPane>
            </items>
        </SplitPane>
        <Button fx:id="addHoursButton" layoutX="24.0" layoutY="329.0" mnemonicParsing="false" prefHeight="35.0" prefWidth="115.0" text="Add Hours" AnchorPane.bottomAnchor="142.0" />
        <Button fx:id="settings" layoutX="24.0" layoutY="426.0" mnemonicParsing="false" prefHeight="35.0" prefWidth="115.0" text="Settings" AnchorPane.bottomAnchor="82.0" />
        <ImageView fx:id="profilePicture" fitHeight="45.0" fitWidth="45.0" layoutX="1.0" layoutY="29.0" pickOnBounds="true" preserveRatio="true" AnchorPane.leftAnchor="2.0" AnchorPane.topAnchor="30.0" />
        <TextFlow fx:id="profileDetails" layoutX="2.0" layoutY="75.0" prefHeight="35.0" prefWidth="101.0" AnchorPane.leftAnchor="2.0" AnchorPane.topAnchor="77.0" />
    </children>
</AnchorPane>

here是我的项目结构 (从未创建过资源选项卡,因为这样我就无法将 Controller 与 fxml 文件连接起来) 我刚刚开始使用 JavaFX,所以我可能没有在这里看到一些常见的东西,但我在网上到处进行了研究,似乎没有什么可以解决这个问题。 顺便说一句,我正在使用 JavaFX 11.02、Java SDK 12 和 IntelliJ IDE

最佳答案

您导入了错误的ListView

查看您的实际异常:

IllegalArgumentException: Can not set javax.swing.text.html.ListView field sample.MainPageController.hoursLog to javafx.scene.control.ListView

通常这意味着您的 FXML 正在定义 JavaFX 控件,但您的实际类正在导入 javax.swing.text.html.ListView而不是javafx.scene.control.ListView

您的问题不包括 MainPageController.java,这是此错误的来源,因此我无法明确回答,但我会查看那里。

Side Note: I do recommend learning the Java naming conventions and sticking to them in the future.

关于java - 当我尝试加载 FXML 文件时,出现 java.io.IOException 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58965332/

相关文章:

java - 使用GUI设计器时如何在Intellij IDEA中设置当前区域设置?

java - 自定义 TableView 以具有 "Add"按钮

java - 逆乘法表javafx

java - 如何通过 Class<?> 来 InitialContext.lookup EJB 实例

java - 在java中旋转图像

java - Gradle 添加库到类路径

javafx - 什么是 JavaFX、FXML 和 Scene Builder?

java - 尝试/最终没有捕获和返回值

java - GlassFish Http 状态 500 - 内部服务器错误

intellij-idea - 如何添加 Meteor 源进行调试