JavaFX 8图像加载(找不到无效的URL或资源)

时间:2015-02-12 22:50:43

标签: java image intellij-idea javafx javafx-8

如关于Image class的那样docs.oracle

// load an image in background, displaying a placeholder while it's loading  
// (assuming there's an ImageView node somewhere displaying this image)  
// The image is located in default package of the classpath
Image image1 = new Image("/flower.png", true);

我在github分享了我的简单项目,因此您可以轻松访问代码 这是我的代码:

splashScreen = new Image("/gravixsplash.png");
splashScreenBackplate = new ImageView();
splashScreenBackplate.setImage(splashScreen);

instructionLayer = new Image("/gravixinstructions.png");
splashScreenTextArea = new ImageView();
splashScreenTextArea.setImage(instructionLayer);

但是这不想为我工作,我明白了:

  

引起:java.lang.IllegalArgumentException:在javafx.scene.image.Image.validateUrl(Image.java:1081)找不到无效的URL或资源

at(Image.java:1081)我发现了这个:

if (resource == null) {
    throw new IllegalArgumentException("Invalid URL or resource not found");
}

我认为我的url(资源)由于某种原因等于null,但它并没有帮助我找到解决方案。

(也许某种程度上我需要打开javaFX8,我怀疑intellijIDEA构建javaFX2项目,我在设置中搜索这个,但没有找到任何东西)

1 个答案:

答案 0 :(得分:0)

项目中的图像不在类路径的根目录中,它们位于:

 /ru/petrsu/javafxGame/

所以而不是:

new Image("/gravixsplash.png")

使用:

new Image("/ru/petrsu/javafxGame/gravixsplash.png")

来自Image javadoc

  

如果传递的字符串不是有效的URL,而是路径,则在该情况下在类路径上搜索Image。