JavaFX java.lang.IllegalStateException : Location is not set

标签 javafx

帮助!我被卡住了..我尝试运行我的主要 javafx 应用程序

这是我的代码;

@Override
public void start(Stage primaryStage) throws Exception {
    try {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("/com/utmkl/fxml/SimulatorDisplay.fxml"));
        Parent content = (Parent)loader.load(); 

    primaryStage.setResizable(false);
    primaryStage.initStyle(StageStyle.UTILITY);
    primaryStage.setScene(new Scene(content));
    primaryStage.show();            
} catch(Exception e) {
    e.printStackTrace();
}

以下是我的文件夹结构:
图片Folder structure

下面是错误
java.lang.IllegalStateException: Location is not set.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
    at com.utmkl.VMCSManager.start(VMCSManager.java:43)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)

最佳答案

我找到了答案..

资源需要与主类位于相同的文件夹结构中

这是我的 Maven-JavaFX 文件夹结构

VMCS
- src/main/java
     - com.utmkl
          VMCSManager.java
- src/main/resources
     - com.utmkl.fxml
          SimulatorDisplay.fxml

所以,正确的代码(运行成功)

虚拟机管理器
@Override
    public void start(Stage primaryStage) throws Exception {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(getClass().getResource("fxml/SimulatorDisplay.fxml"));
            Parent content = loader.load(); 

            Scene scene = new Scene(content);

            primaryStage.setResizable(false);
            primaryStage.initStyle(StageStyle.UTILITY);

            primaryStage.setScene(scene);
            primaryStage.show();            
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

关于JavaFX java.lang.IllegalStateException : Location is not set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43721851/

相关文章:

java - Jruby-Javafx : How to add icon to button?

javafx - 如何让标签的高度与 JavaFX 中的字体大小相同?

css - Javafx 组合框 CSS 填充

android - 如何使 Javafx 中的 TextField 看起来像 Android 或 material UI 一样漂亮

JavaFX:将 child 添加到 ScrollPane

java - JavaFX ListView 中的 WPF ListView.ItemTemplate 等效项是什么?

java - 从用户输入获取双倍值时出错

java - 如何使用 JAVAFX 图表在图表上绘制多轴

java - NSEvent 断言失败?

JavaFX 使用 Gluon SceneBuilder InvocableTargetException