88分王的新问题---在程序中用到非class资源的url问题

alin19 2003-05-11 01:26:06
Toolkit kit = Toolkit.getDefaultToolkit();
welcomeImage = kit.getImage(getClass().getResource("/workbench/pics/welcome.gif"));

一定要这样使用图片,如果不加getClass().getResource()则打包成*.jar
后它将不使用jar文件内的图片,而去寻找/workbench/pics/welcome.gif

getClass().getResource()这两个方法到底是怎么回事情呢,及对他们使用的补充
请指教(我懒的看doc)
...全文
34 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
alexll 2003-06-22
  • 打赏
  • 举报
回复
getResource

public URL getResource(String name)

Finds a resource with a given name. This method returns null if no resource with this name is found. The rules for searching resources associated with a given class are implemented by the * defining class loader of the class.
This method delegates the call to its class loader, after making these changes to the resource name: if the resource name starts with "/", it is unchanged; otherwise, the package name is prepended to the resource name after converting "." to "/". If this object was loaded by the bootstrap loader, the call is delegated to ClassLoader.getSystemResource.


Parameters:
name - name of the desired resource
Returns:
a java.net.URL object.
Since:
JDK1.1
See Also:
ClassLoader
alin19 2003-06-22
  • 打赏
  • 举报
回复
顶一下
alin19 2003-05-12
  • 打赏
  • 举报
回复
还有别的解释吗?
alin19 2003-05-12
  • 打赏
  • 举报
回复
xjffj 2003-05-11
  • 打赏
  • 举报
回复
其实getClass().getResource()在载入资源是ClassLoader在做载入,原理基本和类的载入类似,因此使用的是相对路径。
alin19 2003-05-11
  • 打赏
  • 举报
回复
顶下

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧