java JavaFx 图像路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33305037/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:30:01  来源:igfitidea点击:

JavaFx Images Path

javaimageexceptionjavafxpath

提问by Viktoria

I have a problem with my Java class. Actually the code is correctly, but if I click the run-button there's a exception caused of the path of the image.

我的 Java 类有问题。实际上代码是正确的,但是如果我单击运行按钮,则会出现图像路径导致的异常。

static Image currentBackground = new Image("Snake/Images/background_options.png", true);

And the compiler's message is:

编译器的消息是:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:122)
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
    at javafx.scene.image.Image.validateUrl(Image.java:1100)
    at javafx.scene.image.Image.<init>(Image.java:624)
    at view.OptionsWindow.<clinit>(OptionsWindow.java:21)
    ... 3 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
    at javafx.scene.image.Image.validateUrl(Image.java:1092)
    ... 5 more

Process finished with exit code 1

进程以退出代码 1 结束

Can anybody help me?

有谁能够帮我?

回答by James_D

The Imageconstructoris expecting the specification of a URL, not a file system path. Assuming you are bundling this image as part of your application, you will need to load that from the same place as your classes are loaded: probably a jar file in your final deployment, but perhaps from the file system during development.

Image构造被期待的规范网址,而不是一个文件系统路径。假设您将此映像捆绑为应用程序的一部分,您将需要从加载类的同一位置加载它:可能是最终部署中的一个 jar 文件,但也可能是开发过程中的文件系统。

The mechanism to get a URL representing a resource which is part of the application is to call getResource()on a Classor ClassLoader.

该机制得到代表的资源是应用程序的一部分是调用URLgetResource()ClassClassLoader

The exact way to do this depends on your project structure, which you haven't shown, but for example:

执行此操作的确切方法取决于您尚未显示的项目结构,但例如:

new Image(getClass().getResource("Snake/Images/background_options.png").toString(), true);

will load the image from a resource, specified relative to the current class, and

将从相对于当前类指定的资源加载图像,并且

new Image(getClass().getClassLoader().getResource("Snake/Images/background_options.png").toString(), true);

will load the image from a resource specified relative to the class path.

将从相对于类路径指定的资源加载图像。

In the event you pass a Stringthat represents a relative URL (i.e. one with no scheme, such as file:, http:, or jar:), then the Imageconstructor will search on the class path for the resource. In other words

在事件中,你传递一个String代表相对URL(即一个没有方案,如file:http:jar:),那么Image构造函数将搜索资源类路径上。换句话说

new Image("Snake/Images/background_options.png", true);

is equivalent to

相当于

new Image(getClass().getClassLoader().getResource("Snake/Images/background_options.png").toString(), true);

This seems slightly counter-intuitive (to me, at least), so I prefer to always specify a URL completely, or to retrieve one from getClass().getResource()or File.toURI().toURL()as appropriate.

这似乎有点违反直觉(至少对我来说),所以我更喜欢总是完全指定一个 URL,或者从getClass().getResource()File.toURI().toURL()适当地检索一个 URL 。