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

查看:147
本文介绍了从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();
      }
   }

最大的区别和我没有的原因一直在使用他的代码是因为我的根窗格是一个Anchor窗格而不是一个普通的窗格,他的方式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天全站免登陆