凡把“资源”日食日程表 [英] Where put "resources" dir on eclipse
问题描述
我的项目是一个eclipse Java项目,它的结构是
\Game\src
但是这个代码返回一个例外:
image = new ImageIcon(new URL(resources\\\Monster.png));
java.net.MalformedURLException:no protocol:resources\Monster.png
有什么问题?
在eclipse中,使 / code>文件夹一个Source文件夹,以便在启动应用程序时将文件添加到类路径。
然后使用
image = new ImageIcon(YourClass.class.getResource(/ monster.png));
其中 YourClass
是您的课程。
< blockquote>
查找具有给定名称的资源。
这是通过在应用程序的基于javadoc中描述的一些命名规则的类路径。
My project is a eclipse Java project and it structure is
\Game\src
\Game\resources
But this code returns a exception:
image = new ImageIcon(new URL("resources\\Monster.png"));
java.net.MalformedURLException: no protocol: resources\Monster.png
What's wrong?
In eclipse, make the resources
folder a Source folder so that it adds the files inside it to the classpath when it launches your application.
Then use
image = new ImageIcon(YourClass.class.getResource("/monster.png"));
where YourClass
is your class.
The Class#getResource(String)
call
Finds a resource with a given name.
It does this by looking for it in your application's classpath based on some naming rules described in the javadoc.
这篇关于凡把“资源”日食日程表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!