我正在使用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)
Run Code Online (Sandbox Code Playgroud)
这是我的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);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的启动画面代码:
<?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>
Run Code Online (Sandbox Code Playgroud)
这是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();
}
}
Run Code Online (Sandbox Code Playgroud)
这是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>
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏。谢谢。
在主类中,您的包和类名称为:
tech.faraaz.zoforo.Main
Run Code Online (Sandbox Code Playgroud)
在SplashScreenController中,它是:
tech.faraaz.zoforo.gui.SplashScreenController
Run Code Online (Sandbox Code Playgroud)
因此,这些类位于不同的程序包中。
但是,您尝试使用相同的相对位置来获取资源。
在主要:
getClass().getResource("gui/SplashScreen.fxml")
Run Code Online (Sandbox Code Playgroud)
在SplashScreenController中:
getClass().getResource("gui/OpenAccountScreen.fxml")
Run Code Online (Sandbox Code Playgroud)
因此,相对于SplashController的位置,OpenAccountScreen.fxml必须位于以下位置才能找到它:
tech/faraaz/zoforo/gui/gui/OpenAccountScreen.fxml
Run Code Online (Sandbox Code Playgroud)
我敢打赌它不在那里...
可能,而不是相对于当前类访问FXML,您应该相对于给定类(例如,始终是Main)或通过绝对引用来访问它。这可能有助于防止您感到困惑。
例如,写:
Main.class.getResource("gui/SplashScreen.fxml");
Main.class.getResource("gui/OpenAccountScreen.fxml");
Run Code Online (Sandbox Code Playgroud)
要么:
getClass().getResource("/tech/faraaz/zoforo/gui/SplashScreen.fxml")
getClass().getResource("/tech/faraaz/zoforo/gui/OpenAccountScreen.fxml")
Run Code Online (Sandbox Code Playgroud)
注意,要调试这样的东西,您总是可以运行:
System.out.println(getClass().getResource("gui/OpenAccountScreen.fxml"));
Run Code Online (Sandbox Code Playgroud)
如果显示null,则表明资源不在您期望的位置,则可以从那里进行故障排除。
| 归档时间: |
|
| 查看次数: |
3708 次 |
| 最近记录: |