使用getResources()方法无法访问资源 [英] Cannot access resource by using the getResources() method
问题描述
我有以下项目树:
我可以使用以下命令访问290.gif文件: Java GUI/src/dk/resources/290.gif :
I can access the 290.gif file in the path: Java GUI/src/dk/resources/290.gif by using the following command:
this.getClass().getResource("/dk/resources/290.gif")
但是我无法使用以下命令访问路径中的290.gif文件: Java GUI/resources2/290.gif :
But I can't access the 290.gif file in the path: Java GUI/resources2/290.gif by using the following command:
this.getClass().getResource("/resources2/290.gif")
我正在尝试从HelloWorldFrame.java类访问290.gif文件. 我想访问以下路径中的290.gif文件: Java GUI/resources2/290.gif !
I am trying to access the 290.gif file from the HelloWorldFrame.java class. I want to access the 290.gif file in the path: Java GUI/resources2/290.gif!
我做错了什么?
推荐答案
您已在Eclipse中将src
和resources2
目录都声明为源文件夹.
当Eclipse构建您的应用程序时,它会将资源复制到build
文件夹中,依此类推:
You have declared both src
and resources2
directories as source folders in Eclipse.
When Eclipse builds your app, it copies recources to the build
folder, and so:
-
dk.resources
程序包中的文件可通过/dk/resources/290.gif
访问
-
resources2
文件夹中的文件没有任何软件包,可以通过/290.gif
访问
- the files from
dk.resources
package are accessible via/dk/resources/290.gif
- the files from
resources2
folder are not in any package, and are accessible via/290.gif
如果要通过/resources2/290.gif
访问该文件,则在src
文件夹下创建一个名为resource2
的程序包.
If you want the file to be accessible via /resources2/290.gif
, then create a package named resource2
under the src
folder.
这篇关于使用getResources()方法无法访问资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!