java JavaFX 8 图像加载(无效的 URL 或找不到资源)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28489402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaFX 8 Image load (Invalid URL or resource not found)
提问by Alexander
as said there docs.oracleabout Image class:
如前所述,有关于 Image 类的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);
I shared my simple project at github, so you can easily access code.
and this is piece of my code:
我在github 上分享了我的简单项目,因此您可以轻松访问代码。
这是我的一段代码:
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:
但这对我不起作用,我明白了:
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found at javafx.scene.image.Image.validateUrl(Image.java:1081)
引起:java.lang.IllegalArgumentException: 在 javafx.scene.image.Image.validateUrl(Image.java:1081) 中找不到无效的 URL 或资源
at (Image.java:1081) I find this:
在 (Image.java:1081) 我发现这个:
if (resource == null) {
throw new IllegalArgumentException("Invalid URL or resource not found");
}
I assume that my url(resource) is for some reason equal to null, but it didn't help me find the solution.
我假设我的 url(resource) 由于某种原因等于 null,但它没有帮助我找到解决方案。
(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)
(也许不知何故我需要打开javaFX8,我怀疑intellijIDEA构建javaFX2项目,我在设置中搜索这个,但没有找到任何东西)
回答by jewelsea
The images in your project aren't in the root of your class path, they are in:
项目中的图像不在类路径的根目录中,它们位于:
/ru/petrsu/javafxGame/
So instead of:
所以而不是:
new Image("/gravixsplash.png")
Use:
利用:
new Image("/ru/petrsu/javafxGame/gravixsplash.png")
From the Image javadoc:
If the passed string is not a valid URL, but a path instead, the Image is searched on the classpath in that case.
如果传递的字符串不是有效的 URL,而是路径,则在这种情况下在类路径上搜索图像。