【发布时间】:2018-06-05 00:18:59
【问题描述】:
我知道,这里有很多类似的问题有类似的问题,但是在阅读了大量的帖子之后,我不能简单地解决这个问题。 在 Compiler > Resource Patterns 下,我已经输入了这个字符串“*.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
【问题讨论】:
-
编译成一个jar,打开,看看有没有MainView.fxml。原则上(不会有帮助)使用
MainView.class.getResource而不是getClass().getResource。为您的项目使用 maven 构建基础架构,将具有开箱即用的正确设置:src/main/resources 等。 -
嗨。我正在使用 Gradle,它为我设置了一切。此外,.fxml 文件在生成的 .jar 文件中。
-
Gradle 也提供了一个标准。在命令行上启动 jar?我曾希望会有一个
/MainView.fxml.xml、/resources/MainView.fxml或类似的,但唉。有一次我在文件名中有一个í而不是i。重新输入字符串"/MainView.fxml"可能有一些看不见的垃圾。因为实际上不会有太大的错误。 -
目前我只是通过 IntelliJ Idea 构建和运行程序。我不能简单地理解问题可能是什么。我尝试重写字符串并遵循这篇文章 (stackoverflow.com/questions/20507591/…) 中的提示,他们说将 .fxml 文件放在自定义 /fxml 文件夹中。这个问题让我抓狂。
-
打印出 this.getClass().getResource("./) 返回的 URL 然后检查你的 *.fxml 文件是否在同一个目录中。
标签: java intellij-idea javafx null resources