JavaFX java.lang.IllegalStateException:未设置位置 [英] JavaFX java.lang.IllegalStateException: Location is not set

查看:2057
本文介绍了JavaFX java.lang.IllegalStateException:未设置位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Help! I'm stuck.. I try to run my main javafx app

这是我的代码;

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

以下是我的文件夹结构:
picture 文件夹结构

Below is my folder structure: picture 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)


推荐答案

我找到了答案..

资源需要与主类在同一文件夹结构中

the resource need to be in the same folder structure as main class

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

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

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

So, the correct code (which success to run)

VMCSManager.java

VMCSManager.java

@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:未设置位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆