从 bin 文件夹以外的文件夹加载 fxml 文件时出错 [英] Error loading fxml files from a folder other than the bin folder

查看:30
本文介绍了从 bin 文件夹以外的文件夹加载 fxml 文件时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个相当新的 Java 程序员.我只有大约五周的经验,从零开始,如果它们与控制器类不在同一个文件夹中,我在让在 Scene Builder 中创建的 javafx fxml 文件正确加载时遇到问题.

I am a fairly new java programmer. I only have about five weeks of experience, starting from zero, and I am having problems getting javafx fxml files created in Scene Builder to load correctly if they are not in the same folder as the controller classes.

我正在使用

Win7x64 running jre7x86 for this build

Eclipse Juno Service Release 1
Build id: 20120920-0800

jre version 1.7.0_07

javaFx version 2.2.1-b03

SceneBuilder version 1.0-b50

我的场景加载器代码是

private static final String RESOURCE_PATH = "/resources/";
public static Stage buildStage(@SuppressWarnings("rawtypes") Class pClass,
         String stageTitle, String resourceLocation)
   {
      Stage temp = new Stage();
      Pane page = null;
      try
      {
         page = FXMLLoader.load(pClass.getResource(RESOURCE_PATH + resourceLocation), null,
               new JavaFXBuilderFactory());
      }
      catch (IOException e)
      {
         e.printStackTrace();
      }
      Scene scene = new Scene(page);
      temp.setScene(scene);
      temp.setTitle(stageTitle);
      temp.setResizable(false);
      return temp;
   }

我知道项目目录是 /workspace/projectName/ 所以理论上加载程序应该能够找到文件,如果给定了相对路径,对吗?我得到的错误是

I know that the project directory is the /workspace/projectName/ so theoretically the loader should be able to find the files if it's given a relative path right? The error I'm getting is

Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
    at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
    at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2737)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2721)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2707)
    at outofunit.system.StageFactory.buildStage(StageFactory.java:32)
    at outofunit.desktop.ArcMaster.start(ArcMaster.java:28)
    at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
    at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:206)
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
    ... 1 more

我也尝试为加载程序提供绝对路径,但它只是不接受任何不在同一目录中的内容.我在这里抓着稻草试图弄清楚这一点.

I've tried giving the loader an absolute path as well but it just isn't accepting anything that isn't directly in the same directory. I'm grasping at straws here trying to figure this out.

我试图自己研究这个,但我没有得到太多,这里的几个看起来相似的答案似乎没有帮助,或者我太密集和/或缺乏经验而无法理解他们.它们是 JavaFX 2.0 使用事件处理程序加载 fxml 文件失败JavaFX 2.0 FXML 资源加载错误

I've tried to research this on my own but I haven't gotten much, the few answers on here that seemed similar didn't seem to help, that or I am too dense and/or inexperienced to make sense of them. They are JavaFX 2.0 loading fxml files with event handlers fails and JavaFX 2.0 FXML resource loading error

我的一个朋友使用这个代码解决了这个问题

A buddy of mine was able to overcome this problem by using this code

public void load(String pFileName, Stage pStage, String pTitle)
   {
      String fName = RESOURCE_PATH + pFileName;

      try
      {
         String externalForm = getClass().getResource(fName)
            .toExternalForm();

         InputStream inStream = new URL(externalForm).openStream();

         FXMLLoader loader = new FXMLLoader();
         Pane p = (Pane)loader.load(inStream);

         pStage.setTitle(pTitle);
         pStage.setScene(new Scene(p));

         mWindowControl = loader.getController();
         mWindowControl.setUp(pStage);

         pStage.show();
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
   }

最大的区别和我没有使用他的代码的原因是因为我的根窗格是一个锚窗格而不是一个普通窗格,他的方式 URL instream 强制一个普通窗格.不使用普通窗格作为我的基础是我的错吗?我希望你们能提供任何帮助或意见.谢谢.

The biggest difference and the reason why I haven't been using his code is because my root pane is an Anchor pane instead of a normal pane, and his way URL instream forces a normal pane. Am I at fault for not using a normal pane as my base? I would love any help or input you guys can give. Thank you.

所以我一直在解决这个问题,错误信息已经改变.

So I've been working on this problem and the error message has changed.

我把代码改成了这个

private final String RESOURCE_PATH = "resources/";
public void load(String pFileName, Stage pStage, String pTitle)
   {
      String fName = RESOURCE_PATH + pFileName;

      Pane page = null;

      FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(fName));

      try
      {
         page = (Pane) fxmlLoader.load();
      }
      catch (IOException exception)
      {
         throw new RuntimeException(exception);
      }

      Scene scene = new Scene(page);
      pStage.setScene(scene);
      pStage.setTitle(pTitle);

      mWindowControl = fxmlLoader.getController();
      mWindowControl.setUp(pStage);

      pStage.show();
   }

但我现在得到的错误是

java.lang.IllegalStateException: Location is not set.
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2021)
    at outofunit.desktop.WindowLoader.load(WindowLoader.java:136)
    at outofunit.desktop.WindowLoader.load(WindowLoader.java:62)
    at outofunit.desktop.ArcMaster.start(ArcMaster.java:30)
    at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
    at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:206)
    at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:173)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
    at java.lang.Thread.run(Unknown Source)

我不明白如何未设置位置

I don't understand how the location is not being set

推荐答案

两个例外都表示在您提供的位置找不到您尝试加载的 (FXML) 文件".您的文件名或路径错误.提供您的包结构或至少包含 load(String pFileName, Stage pStage, String pTitle) 方法的类文件的路径以及您要加载的 FXML 文件的路径.阅读 getResource() 的 javadoc API 并检查网上关于它的示例代码.

Both exceptions say "the (FXML) file you are trying to load cannot be found in the location you have provided". Your filename is wrong or its path. Give your package structure or at least the path of class file that includes load(String pFileName, Stage pStage, String pTitle) method and the path of FXML file you want to load. Read the javadoc API of getResource() and examine sample codes about it on the net.

这篇关于从 bin 文件夹以外的文件夹加载 fxml 文件时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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