如何在Applet中引用jar中的资源文件

jbaquevz739

jbaquevz739

2016-02-19 16:28

下面图老师小编跟大家分享如何在Applet中引用jar中的资源文件,一起来学习下过程究竟如何进行吧!喜欢就赶紧收藏起来哦~

  如果想要做一个比较漂亮的Applet让人家使用,一定会加上很多资源,比如图片或者声音文件什么的。

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

  sun提供了一个有用的工具,jar。这个工具可以把这些资源文件合在一个文件里,避免频繁的http request,

  而且下载的jar文件可以被缓存,很爽吧。

  但是如何正确引用jar中的资源呢?

  比如我们打算显示一个图片按钮,图片相对路径为./img/Logo.gif,你可以自己随便找一个gif图片。

  让我们来看看我们想当然的做法。

  

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ImageButtonApplet extends JApplet
{
private String path = "/img/Logo.gif";
private ImageIcon LogoButtonIcon = new ImageIcon(path);
/**Initialize the applet*/
public void init()
{
try
{
if (LogoButtonIcon == null)
throw new Exception("cannot get the image!");
JButton iButton = new JButton(LogoButtonIcon);
Container cp = this.getContentPane();
cp.add(iButton);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

  这样子编译之后,把ImageButtonApplet.class和Logo.gif保持相对路径打进jar里面,对应的HTML页面代码为APPLET CODE = "ImageButtonApplet.class" CODEBASE = "." ARCHIVE = "test.jar" WIDTH = "200" HEIGHT = "200"/APPLET,由于使用了Swing,

  经过HTMLConverter预处理之后,本以为能够一举成功,打开页面却发现,抛出异常:

  java.security.AccessControlException: access denied (java.io.FilePermission /img/Logo.gif read)

  这件事情也郁闷了我很久,反复试验,不管path相对路径还是什么,都不能顺利实现。

  后来我研究了jdk自带的demo,发现demo在引用资源的时候,采用这样的方法 getClass().getResource(String sourceName);

  getClass()是Object的方法,返回一个对象的运行时类型,即CLass对象。

  原来Class对象有getResource方法,在API文档中就是这样写的:

  

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

  如法炮制,我把原来的

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/bianchengyuyan/)

  private ImageIcon LogoButtonIcon = new ImageIcon(path);

  改成

  private ImageIcon LogoButtonIcon = new ImageIcon(getClass().getResource(path));

  编译,jar,run,成功,无论是本机打开还是放到http服务器中,都没有问题了。

  这就是在Applet中引用jar中资源文件的KEY!

展开更多 50%)
分享

猜你喜欢

如何在Applet中引用jar中的资源文件

编程语言 网络编程
如何在Applet中引用jar中的资源文件

播放资源文件文件中的声音

编程语言 网络编程
播放资源文件文件中的声音

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

如何在VB6.0中创建和使用文本资源文件

编程语言 网络编程
如何在VB6.0中创建和使用文本资源文件

资源文件在DELPHI中的使用

Delphi
资源文件在DELPHI中的使用

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

vc在应用程序中播放声音资源文件

编程语言 网络编程
vc在应用程序中播放声音资源文件

MAC中如何通过Automator解压jar文件

电脑入门
MAC中如何通过Automator解压jar文件

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

一个记事本的源程序

一个记事本的源程序

分解如何排除Windows XP启动故障

分解如何排除Windows XP启动故障
下拉加载更多内容 ↓