【问题标题】:JavaFX 8 Image load (Invalid URL or resource not found)JavaFX 8 图像加载(无效的 URL 或找不到资源)
【发布时间】:2015-04-13 21:25:58
【问题描述】:

正如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);

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

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

在 (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("/ru/petrsu/javafxGame/gravixsplash.png")
  • @jewelsea thx,现在一切正常!非常感谢!

标签: java image intellij-idea javafx javafx-8


【解决方案1】:

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

 /ru/petrsu/javafxGame/

所以而不是:

new Image("/gravixsplash.png")

用途:

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

来自Image javadoc

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-02
    • 2013-06-04
    • 1970-01-01
    • 2019-04-30
    • 2016-10-23
    • 1970-01-01
    • 2020-01-05
    • 2021-03-18
    相关资源
    最近更新 更多