java - IntelliJ Idea + JavaFX = class.getResource() 返回 null

标签 java intellij-idea javafx null resources

我知道,这里有很多类似的问题,但在阅读了大量的帖子后,我无法简单地解决这个问题。 在编译器>资源模式下,我已经放置了这个字符串“*.fxml”。此外,我已将资源文件夹标记为资源根。

在这里您可以看到我的项目结构:

Project Structure

这是我的MainView.java类。

package dataParser.View;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;


public class MainView extends Application {
    private Stage primaryStage;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        this.primaryStage = primaryStage;

        FXMLLoader loader = new FXMLLoader();
        System.out.println(this.getClass().getResource("/MainView.fxml"));
        loader.setLocation(getClass().getResource("/MainView.fxml"));

        MainViewController mainViewController = new MainViewController();
        mainViewController.setMainApp(this);

        loader.setController(mainViewController);
        Parent layout = loader.load();

        Scene scene = new Scene(layout);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    Stage getPrimaryStage() {
        return primaryStage;
    }

}

不过,我只是取回一个空资源。有什么提示吗?
谢谢!

编辑
这是完整的堆栈跟踪。我还注意到链接所有资源文件时遇到问题。

    Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
    at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.lang.IllegalStateException: Location is not set.
    at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2459)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
    at dataParser.View.MainView.start(MainView.java:29)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
    ... 1 more

最佳答案

过了一段时间,我找到了这个棘手问题的解决方案,我将尽力在这里解释它。

首先,我以正确的方式设置了项目结构(文件 > 项目结构...),定义了源文件夹和资源文件夹,以便 IntelliJ Idea 能够找到它们。此外,我检查了我的资源文件是否已正确放入“build”和“out”文件夹中(它们在构建过程中由 Gradle 和 IntelliJ 使用):我还通过设置我的“File|”来实现它。许多 Stack Overflow 答案中都明确指出了“设置|编译器”设置。
最后,我在调用资源文件时使用了这个结构:

FXMLLoader loader = new FXMLLoader();
System.out.println(MainView.class.getResource("/fxml/MainView.fxml"));
loader.setLocation(MainView.class.getResource("/fxml/MainView.fxml"));

我想确定我的 fxml 文件夹将 Resources 作为根文件夹。

关于java - IntelliJ Idea + JavaFX = class.getResource() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47939908/

相关文章:

java - JetBrains 如何为其 IDE 创建 .exe 文件?

java - 如何使用 Spring Boot 和 IntelliJ Idea 从数据库生成实体类?

java - JavaFX 8 中文本区域的透明背景

java - 在 fxml 布局中使用常量

java - IntelliJ IDE 上的 SDK 错误消息

javafx - 如何导出 tornadofx 应用程序?

java - 更改二进制字符串中第一位的值 - Java

java - 如何用docx4j替换标题和表格中的变量?

java - 有没有替代分层 Spring 安全角色的方法?

java - H2内存数据库和自定义@GenericGenerator策略