英文:
I'm trying to import an fxml file into my main class and Parent root = FXMLLoader.load(getClass().getResource()); returns null
问题
Parent root = FXMLLoader.load(getClass().getResource("/view/Main_Form.fxml")); 返回 null。
它基本上告诉我找不到我要找的 "Main_Form.fxml" 文件。
我把它放在一个名为 "View" 的包中,这就是我指定的路径。
以下是代码:
package com.example.c482_1;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class HelloApplication extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/view/Main_Form.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
项目的文件结构:项目文件结构
我已经尝试检查路径,并将我的主项目设置为资源根目录,但没有成功。
英文:
I'm trying to import an fxml file into my main class and Parent root = FXMLLoader.load(getClass().getResource(/View/Main_Form.fxml)); returns null.
It basically is telling me that it can't find the "Main_Form.fxml" file that I am asking it to find.
I have it in a package labeled "View" which is the path I specified.
code is as follows:
`package com.example.c482_1;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class HelloApplication extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/view/Main_Form.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}`
I have tried checking the path, I made my main project the resource root with no luck as well.
答案1
得分: 1
不确定,但我会尝试检查确保路径是相对的。
英文:
Not quite sure but I would try checking to make sure the path is relative
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论