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

JavaFX 8 Image load (Invalid URL or resource not found)

关于图片 docs.oracle 所说 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 上分享了我的简单项目,因此您可以轻松访问代码。
这是我的一段代码:

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)

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

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

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

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

您项目中的图像不在您的 class 路径的根目录中,它们在:

 /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.