FXMLLoader 没有 find.fxml 资源内的文件
FXMLLoader doesn't find.fxml file inside of resources
我的 FXMLLoader
似乎没有在资源文件夹中找到我的 fxml 文件。
我正在使用 gradle 来 运行 我的项目。由于结构已经给出,我创建了一个新场景,现在正在尝试加载它。
主线:
public class App extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("LoginWindow.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException ignore) {
// ignore for the time being
}
}
}
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.2"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.pm2.SportHub.controller.LoginController">
<children>
<Label layoutX="14.0" layoutY="14.0" text="Hallo Welt" />
</children>
</AnchorPane>
报错信息如下:
java.lang.reflect.InvocationTargetException
Caused by: java.lang.RuntimeException: Exception in Application start method
Caused by: java.lang.IllegalStateException: Location is not set.
理论上这应该可以正常工作,但事实并非如此。有人可以解释我做错了什么吗?
注意:LoginController.java
在一个名为 controller 的包中,class 目前是空的。
试试这个:
Parent root;
@Override
public void start(Stage primaryStage) throws Exception {
try{
root = FXMLLoader.load(getClass().getResource("LoginWindow.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
}
catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
我的 FXMLLoader
似乎没有在资源文件夹中找到我的 fxml 文件。
我正在使用 gradle 来 运行 我的项目。由于结构已经给出,我创建了一个新场景,现在正在尝试加载它。
主线:
public class App extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("LoginWindow.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException ignore) {
// ignore for the time being
}
}
}
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.2"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="ch.zhaw.pm2.SportHub.controller.LoginController">
<children>
<Label layoutX="14.0" layoutY="14.0" text="Hallo Welt" />
</children>
</AnchorPane>
报错信息如下:
java.lang.reflect.InvocationTargetException
Caused by: java.lang.RuntimeException: Exception in Application start method
Caused by: java.lang.IllegalStateException: Location is not set.
理论上这应该可以正常工作,但事实并非如此。有人可以解释我做错了什么吗?
注意:LoginController.java
在一个名为 controller 的包中,class 目前是空的。
试试这个:
Parent root;
@Override
public void start(Stage primaryStage) throws Exception {
try{
root = FXMLLoader.load(getClass().getResource("LoginWindow.fxml"));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
}
catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}