我正在为一家使用 JavaFX 的会计师事务所提出申请。单击我通过 FXML 设置的按钮时出现奇怪的错误。
java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
这是我的 Main.java 代码:
package tech.faraaz.zoforo;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("gui/SplashScreen.fxml"));
Scene scene = new Scene(root);
stage.setTitle("Zoforo");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
这是我的初始屏幕代码:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="410.0" maxWidth="410.0" minHeight="410.0" minWidth="410.0" prefHeight="400.0" prefWidth="401.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="tech.faraaz.zoforo.gui.SplashScreenController">
<children>
<ImageView fitHeight="150.0" fitWidth="200.0" layoutX="24.0" layoutY="23.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../assets/logo.png" />
</image>
</ImageView>
<ListView layoutX="24.0" layoutY="158.0" prefHeight="200.0" prefWidth="200.0" />
<Button layoutX="248.0" layoutY="172.0" mnemonicParsing="false" onMouseClicked="#openAccountWindow" prefHeight="27.0" prefWidth="139.0" text="Open Account" />
<Button layoutX="248.0" layoutY="209.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="139.0" text="New Account" />
<Button layoutX="248.0" layoutY="313.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="139.0" text="Scan Statement" />
<Button layoutX="248.0" layoutY="276.0" mnemonicParsing="false" prefHeight="27.0" prefWidth="139.0" text="Open Statement" />
<Label layoutX="24.0" layoutY="374.0" text="Copyright Zoforo 2017 | zoforo.com" />
<TextField layoutX="24.0" layoutY="122.0" promptText="Search Profiles..." />
</children>
</AnchorPane>
这是 SplashScreenController.java 中的代码。我正在使用“事件”而不是“ActionEvent”。
@FXML
public void openAccountWindow(Event event) throws Exception {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("gui/OpenAccountScreen.fxml"));
Parent root = (Parent) loader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root));
stage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
这是 OpenAccountScreen.java 的代码。
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="384.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
<children>
<ImageView fitHeight="71.0" fitWidth="169.0" layoutX="14.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../assets/logo.png" />
</image>
</ImageView>
<TableView layoutX="14.0" layoutY="137.0" prefHeight="200.0" prefWidth="572.0">
<columns>
<TableColumn prefWidth="204.0" text="Account Name" />
<TableColumn minWidth="0.0" prefWidth="218.0" text="Account Number" />
<TableColumn minWidth="8.0" prefWidth="149.0" text="Statements" />
</columns>
</TableView>
<Button layoutX="480.0" layoutY="346.0" mnemonicParsing="false" text="Open Account" />
<TextField layoutX="14.0" layoutY="101.0" promptText="Search Accounts..." />
</children>
</AnchorPane>
感谢任何帮助。谢谢。
最佳答案
在您的主类中,您的包和类名称是:
tech.faraaz.zoforo.Main
在 SplashScreenController 中是:
tech.faraaz.zoforo.gui.SplashScreenController
所以这些类在不同的包中。
但您尝试使用相同的相对位置获取资源。
主要内容:
getClass().getResource("gui/SplashScreen.fxml")
在 SplashScreenController 中:
getClass().getResource("gui/OpenAccountScreen.fxml")
因此相对于 SplashController 的位置,OpenAccountScreen.fxml 需要位于以下位置才能找到它:
tech/faraaz/zoforo/gui/gui/OpenAccountScreen.fxml
我打赌它不在那里......
可能,而不是访问相对于当前类的 FXML,您应该相对于给定的类(例如总是 Main)或通过绝对引用来访问它。这可能有助于防止您的混淆。
例如写:
Main.class.getResource("gui/SplashScreen.fxml");
Main.class.getResource("gui/OpenAccountScreen.fxml");
或者:
getClass().getResource("/tech/faraaz/zoforo/gui/SplashScreen.fxml")
getClass().getResource("/tech/faraaz/zoforo/gui/OpenAccountScreen.fxml")
注意,要调试这样的东西,你总是可以运行:
System.out.println(getClass().getResource("gui/OpenAccountScreen.fxml"));
如果它打印出 null
,那么您就知道该资源不在您期望的位置,然后您可以从那里进行故障排除。
关于javafx - JavaFX 应用程序中的 "java.lang.IllegalStateException: Location is not set",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45315121/