正如那里所说的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);
但这不想为我工作,我明白了:
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found at javafx.scene.image.Image.validateUrl(Image.java:1081)
在(Image.java:1081)我发现这个:
if (resource == null) {
throw new IllegalArgumentException("Invalid URL or resource not found");
}
我假设我的 url(resource) 由于某种原因等于 null,但这并没有帮助我找到解决方案。
(也许我需要打开javaFX8,我怀疑intellijIDEA构建javaFX2项目,我在设置中搜索这个,但没有找到任何东西)
最佳答案
项目中的图像不在类路径的根目录中,它们位于:
/ru/petrsu/javafxGame/
所以代替:
new Image("/gravixsplash.png")
用途:
new Image("/ru/petrsu/javafxGame/gravixsplash.png")
来自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.
关于JavaFX 8 图像加载(无效 URL 或未找到资源),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28489402/