我知道,这里有很多类似的问题,但在阅读了大量的帖子后,我无法简单地解决这个问题。 在编译器>资源模式下,我已经放置了这个字符串“*.fxml”。此外,我已将资源文件夹标记为资源根。
在这里您可以看到我的项目结构:
这是我的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/