I'm trying to import an fxml file into my main class and Parent root = FXMLLoader.load(getClass().getResource()); returns null

huangapple go评论83阅读模式
英文:

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();
    }
}`

file structure of the project

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

huangapple
  • 本文由 发表于 2023年2月14日 08:55:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442542.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定