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

标签 java image intellij-idea javafx javafx-8

正如那里所说的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/

相关文章:

java - 使用插入排序降序?

python - 如何使用 PIL 获取图片大小?

Java - 无法将 lambda 用于自制接口(interface) - lambda 转换的目标类型必须是接口(interface)

java - Lombok IntelliJ IDEA 插件 : Use of var is disabled by default

intellij-idea - Intellij .ipr 文件 checkin 还是忽略?

java - 如何制作一个将打开计算器应用程序 "X"次的 Java 程序

java - 在 ant build.xml 中包含 Jar

java - 分析希尔排序算法(大O)

javascript - JQuery 不会删除我的 div 来响应 div 单击事件

java - Java无法访问包外资源