当前位置: 网学 > 编程文档 > JAVA > 正文

Java编程小技巧集锦

来源:Http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 12/10/16
下载{$ArticleTitle}原创论文样式
有ClassLoader, demo在引用资源的时候采用方法 getClass().getResource(String sourceName)

如下:

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.

*/
     /**Initialize the applet*/
     public void init(){
     try {
          if (myButtonIcon == null)
          throw new Exception("cannot get the image!");
          JButton iButton = new JButton(myButtonIcon);
          Container cp = this.getContentPane();
          cp.add(iButton);
     }
     catch (Exception e){
        e.printStackTrace();
     }
}

}

子编译之后,把ImageButtonApplet.class和my.gif保持相对路径打进jar里面,对应的HTML页面代码为<APPLET CODE = "ImageButtonApplet.class" CODEBASE = "." ARCHIVE = "my.jar" WIDTH = "200" HEIGHT = "200"></APPLET>。成功关键在于使用getClass().getResource(path).

网学推荐

免费论文

原创论文

浏览:
设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved
湘ICP备09003080号