JavaFx图像路径 [英] JavaFx Images Path
问题描述
static Image currentBackground = new Image(Snake / Images / background_options.png,true);
编译器的消息是:
线程main中的异常java.lang.ExceptionInInitializerError
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Class .java:264)
在com.intellij.rt.execution.application.AppMain.main(AppMain.java:122)
导致:java.lang.IllegalArgumentException:无效的URL:无效的URL或资源没有找到
在javafx.scene.image.Image.validateUrl(Image.java:1100)
在javafx.scene.image.Image。< init>(Image.java:624)
在view.OptionsWindow。< clinit>(OptionsWindow.java:21)
... 3更多
导致:java.lang.IllegalArgumentException:找不到URL或资源
javafx.scene.image.Image.validateUrl(Image.java:1092)
...另外
进程完成退出代码1
有人可以帮助我吗?
Image
构造函数期待一个 URL ,而不是文件系统路径。假设您将该映像作为应用程序的一部分捆绑在一起,您将需要从与加载类相同的位置加载该映像:可能是最终部署中的一个jar文件,但也可能在开发过程中来自文件系统。
获取代表应用程序一部分的资源的URL的机制是在 getResource() =http://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getResource-java.lang.String- =nofollow> 类
或 ClassLoader
。
确切的方法取决于您的项目结构,您没有显示,但例如:
新图像(getClass()。getResource(Snake /image/background_options.png)。toString(),true);
将从相对于当前类指定的资源加载图像,
new Image(getClass()。getClassLoader()。getResource(Snake / Images / background_options.png)toString(),true);
将从相对于类路径指定的资源加载图像。
如果您传递一个 String
,表示相对URL(即没有方案的一个,例如 file:
, http:
或 jar:
),然后图像
构造函数将在类路径上搜索资源。换句话说,
新图像(Snake / Images / background_options.png,true);
相当于
new Image(getClass()。getClassLoader()。getResource(Snake / Images / background_options.png)。toString(),true);
这似乎有点直观(至少对我来说),所以我更喜欢总是指定一个URL完整,或从 getClass()中获取一个。getResource()
或 File.toURI()。toURL()
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.
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
Can anybody help me?
The Image
constructor is 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.
The mechanism to get a URL representing a resource which is part of the application is to call getResource()
on a Class
or ClassLoader
.
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 String
that represents a relative URL (i.e. one with no scheme, such as file:
, http:
, or jar:
), then the Image
constructor will search on the class path for the resource. In other words
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.
这篇关于JavaFx图像路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!