Sam*_*uel 2 javafx fxml scenebuilder
我是一个相当新的java程序员.我只有大约五周的经验,从零开始,如果它们与控制器类不在同一个文件夹中,我在使用Scene Builder中创建的javafx fxml文件正确加载时遇到问题.
我在用
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
Run Code Online (Sandbox Code Playgroud)
我的场景加载器代码是
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;
}
Run Code Online (Sandbox Code Playgroud)
我知道项目目录是/workspace/projectName/理论上的,如果给定一个相对路径,加载器应该能够找到文件吗?我得到的错误是
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
Run Code Online (Sandbox Code Playgroud)
我已经尝试给加载器一个绝对路径,但它只是不接受任何不直接在同一目录中的东西.我正在抓住稻草,试图解决这个问题.
我试图自己研究这个,但我没有得到太多,这里似乎相似的几个答案似乎没有帮助,或者我太密集和/或没有经验去理解它们.它们是JavaFX 2.0加载fxml文件,事件处理程序失败,JavaFX 2.0 FXML资源加载错误
我的一个伙伴能够通过使用此代码克服此问题
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();
}
}
Run Code Online (Sandbox Code Playgroud)
我没有使用他的代码的最大区别和原因是因为我的根窗格是一个Anchor窗格而不是普通窗格,他的方式URL instream强制一个普通窗格.我没有使用普通窗格作为我的基础吗?我很乐意为你们提供任何帮助或意见.谢谢.
编辑:所以我一直在研究这个问题,错误信息已经改变.
我将代码更改为此
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();
}
Run Code Online (Sandbox Code Playgroud)
但我现在得到的错误是
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)
Run Code Online (Sandbox Code Playgroud)
我不明白该位置是如何设置的
小智 7
这是一个路径问题,java需要知道从包层次结构中获取文件的位置.所以使用"/package1/resourcename.fxml"应该从树的根找到源.您通常"/"在开始时离开,因为您已经位于包树的顶部.
| 归档时间: |
|
| 查看次数: |
28866 次 |
| 最近记录: |