当前位置: 首页 > 面试题库 >

FXML加载异常

封德华
2023-03-14
问题内容

我遇到了一个我绝对无法独自解决的问题,因为我刚刚开始使用JAVA
FX。我得到了一个讨厌的javafx.fxml.LoadException:,但是我的操作完全像一个指南,但是我无法运行Main。这是异常输出:

apr 07, 2014 4:06:37 EM application.Main start
ALLVARLIG: null
javafx.fxml.LoadException: 
/C:/Users/Jakob/Dropbox/java_kurser/Project%20Timeline/bin/application/LoginGUI.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
    at javafx.fxml.FXMLLoader.load(Unknown Source)
    at application.Main.start(Main.java:21)
    at com.sun.javafx.application.LauncherImpl$8.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$7.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$6$1.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$6$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$6.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$300(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$4$1.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[2,6]
Message: Bearbetningsinstruktionens målmatchning "[xX][mM][lL]" är inte tillåten.
    at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(Unknown Source)
    at javax.xml.stream.util.StreamReaderDelegate.next(Unknown Source)
    ... 20 more

LoginController.java

package application;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;


public class LoginController implements Initializable {

    @FXML // ResourceBundle that was given to the FXMLLoader
    private ResourceBundle resources;

    @FXML // URL location of the FXML file that was given to the FXMLLoader
    private URL location;

    @FXML // fx:id="loginButton"
    private Button loginButton; // Value injected by FXMLLoader

    @FXML // fx:id="newUserButton"
    private Button newUserButton; // Value injected by FXMLLoader

    @FXML // fx:id="passwordField"
    private PasswordField passwordField; // Value injected by FXMLLoader

    @FXML // fx:id="usernameField"
    private TextField usernameField; // Value injected by FXMLLoader

    public void initialize(URL location, ResourceBundle resources) {

        assert loginButton != null : "fx:id=\"loginButton\" was not injected: check your FXML file 'LoginGUI.fxml'.";
        assert newUserButton != null : "fx:id=\"newUserButton\" was not injected: check your FXML file 'LoginGUI.fxml'.";
        assert passwordField != null : "fx:id=\"passwordField\" was not injected: check your FXML file 'LoginGUI.fxml'.";
        assert usernameField != null : "fx:id=\"usernameField\" was not injected: check your FXML file 'LoginGUI.fxml'.";


        //The button event for the login button
        loginButton.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent e)   {
                System.out.println("This button works");
            }
        });
        }
    }

LoginGUI.fxml

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


<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>
<?scenebuilder-background-color 0x008effff?>

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="LoginController">
  <children>
    <Button fx:id="loginButton" cancelButton="false" contentDisplay="CENTER" defaultButton="true" layoutX="254.0" layoutY="263.0" mnemonicParsing="false" opacity="1.0" prefHeight="28.0" prefWidth="92.0" text="Login" underline="false">
      <font>
        <Font size="14.0" fx:id="x1" />
      </font>
    </Button>
    <PasswordField fx:id="passwordField" layoutX="241.0" layoutY="206.0" prefHeight="23.000099999997474" prefWidth="118.0" promptText="Password" />
    <TextField fx:id="usernameField" layoutX="241.0" layoutY="166.0" prefHeight="23.0" prefWidth="118.0" promptText="Username" />
    <Label layoutX="248.0" layoutY="98.0" prefHeight="35.000099999997474" prefWidth="105.0" text="Welcome">
      <font>
        <Font size="22.0" />
      </font>
    </Label>
    <Button id="loginButton" fx:id="newUserButton" cancelButton="false" contentDisplay="CENTER" defaultButton="true" font="$x1" layoutX="254.0" layoutY="313.0" mnemonicParsing="false" opacity="1.0" prefHeight="28.0" prefWidth="92.0" text="New User" underline="false" />
  </children>
</AnchorPane>

和Main.java

package application;

import java.util.logging.Level;
import java.util.logging.Logger;

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

public class Main extends Application {

    public static void main(String[] args) {
        Application.launch(Main.class, (java.lang.String[])null);
    }

    @Override
    public void start(Stage primaryStage) {
        try {
            AnchorPane root = (AnchorPane) FXMLLoader.load(Main.class.getResource("LoginGUI.fxml"));
            Scene scene = new Scene(root);
            primaryStage.setScene(scene);
            primaryStage.setTitle("Bluerift Timeline");
            primaryStage.show();
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}

问题答案:

问题出在源头

因此,您必须将其更改为适当的

这样做,在这里编辑fxml文件的源代码

AnchorPane root = (AnchorPane) FXMLLoader.load(Main.class.getResource("LoginGUI.fxml"));

与这个

AnchorPane root = (AnchorPane) FXMLLoader.load(Main.class.getResource("/packagename/LoginGUI.fxml"));


 类似资料:
  • 我一直在做一个桌面应用程序,在那里我需要使用JavaFX。我在使用Scene Builder构建的eclipse上的JavaFX项目中创建了各种FXML文件。除了这个讨厌的文件(fxmlimglist.FXML)外,我加载任何FXML文件都没有问题。 其主要思想是,当按下一个按钮时,会出现一个新窗口。下面是该按钮的事件处理程序的代码: package explorer中的项目如下所示: https

  • 问题内容: 我想在我的应用程序中加载一个fxml文件。我使用下一个代码: 使用一些fxml,一切正常,与其他人我得到此异常: 我不明白为什么会收到这个例外。 谢谢。 编辑: 添加包括示例: 我的父母fxml 我的包含文件: 问题答案: 这是解决方案。 为加载程序添加以下行: 非常糟糕的错误。

  • 我的应用程序有选项卡式窗格,所以为了保持fxml文件的可管理性,我有一个包含选项卡的主fxml文件,并为每个其他选项卡单独设置一个fxml。这很好,但出于某种原因,应用程序已经停止加载第二个标签。我试着在主应用程序中单独加载它,效果很好。我尝试创建一个新的fxml文件作为测试,并加载它,这也是有效的。但是,它不会将其加载到第二个选项卡中。此外,控制台没有输出。 第二个: 很抱歉代码太长,并提前感谢

  • 项目/ SRC/ 主/ Java/ 资源/ baselayout.fxml 不是应该有用吗?我本来工作得很好,但突然就不是了。我不知道还能做什么。

  • 大家好,我是fxml的新手,所以请忽略我的愚蠢的问题,这里有几个东西,我尝试了两天,但没有成功 > 从表中删除空白,即表大小应达到可用行数(行数不同) 当用户单击表行(任何显示值)时,新的fxml文件将在定位窗格(显示tableview)中打开,分配给tableview,但我想在主视图中显示它(这里显示的是整个表和两个文本字段以及主堆栈后面的搜索按钮)主视图(其他只有标题和侧栏的fxml文件) 如

  • 问题内容: 我正在尝试加载FXML文件并将其显示为应用程序窗口,但出现异常。FXML文件是由FXML Scene Builder创建的。 这是班级的代码 和FXML文件 这是我得到的例外 我究竟做错了什么? 问题答案: 如果无法在运行时类路径(而不是当前目录等)上找到资源,则简短答案是静默返回。 因此,这取决于你的IDE项目设置,如果你使用的是eclipse,请尝试添加运行配置中的文件夹。 一些想