JavaFX 8图像加载(找不到无效的URL或资源) [英] JavaFX 8 Image load (Invalid URL or resource not found)

查看:3905
本文介绍了JavaFX 8图像加载(找不到无效的URL或资源)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

docs.oracle about Image class:

as said there docs.oracle about Image class:

// 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 ,因此您可以轻松访问代码。

这是我的代码片段:

I shared my simple project at github, so you can easily access code.
and this is piece of my code:

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

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

但这不适合我,我明白了:

But this don't wanna work for me, I get this:


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

Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found at javafx.scene.image.Image.validateUrl(Image.java:1081)

at(Image.java:1081)我发现:

at (Image.java:1081) I find this:

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

我假设我的网址(资源)由于某种原因等于null,但它没有帮助我找到解决方案。

I assume that my url(resource) is for some reason equal to null, but it didn't help me find the solution.

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

(Maybe somehow I need to turn on javaFX8, i suspect that intellijIDEA build javaFX2 project, I was searching for this in settings, but didn't find anything)

推荐答案

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

The images in your project aren't in the root of your class path, they are in:

 /ru/petrsu/javafxGame/

所以代替:

new Image("/gravixsplash.png")

使用:

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

来自

From the Image javadoc:


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

If the passed string is not a valid URL, but a path instead, the Image is searched on the classpath in that case.

这篇关于JavaFX 8图像加载(找不到无效的URL或资源)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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