JAVA Applet教程-如何在Applet中引用jar中的资源文件 作者:admin
来源:互联网 发布:那个软件抢火车票最快 编辑:程序博客网 时间:2024/05/21 09:22
如果想要做一个比较漂亮的Applet让人家使用,一定会加上很多资源,比如图片或者声音文件什么的。sun提供了一个有用的工具,jar。这个工具可以把这些资源文件合在一个文件里,避免频繁的http request,而且下载的jar文件可以被 ... <!--
关键字:[标签:SY_关键字]
-->如果想要做一个比较漂亮的Applet让人家使用,一定会加上很多资源,比如图片或者声音文件什么的。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 Sing path = "/img/logo.gif";
private ImageIcon logoButtonIcon = new ImageIcon(path);
/**Initialize the applet*/
public void init()
{
y
{
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.AccessConolException: access denied (java.io.FilePermission /img/logo.gif read)
这件事情也郁闷了我很久,反复试验,不管path相对路径还是什么,都不能顺利实现。
后来我研究了jdk自带的demo,发现demo在引用资源的时候,采用这样的方法 getClass().getResource(Sing sourceName);
getClass()是Object的方法,返回一个对象的运行时类型,即CLass对象。
原来Class对象有getResource方法,在API文档中就是这样写的:
public URL getResource(Sing 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 bootsap 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
如法炮制,我把原来的
private ImageIcon logoButtonIcon = new ImageIcon(path);
改成
private ImageIcon logoButtonIcon = new ImageIcon(getClass().getResource(path));
编译,jar,run,成功,无论是本机打开还是放到http服务器中,都没有问题了。
这就是在Applet中引用jar中资源文件的KEY!
但是如何正确引用jar中的资源呢?
比如我们打算显示一个图片按钮,图片相对路径为./img/logo.gif,你可以自己随便找一个gif图片。
让我们来看看我们想当然的做法。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ImageButtonApplet extends JApplet
{
private Sing path = "/img/logo.gif";
private ImageIcon logoButtonIcon = new ImageIcon(path);
/**Initialize the applet*/
public void init()
{
y
{
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.AccessConolException: access denied (java.io.FilePermission /img/logo.gif read)
这件事情也郁闷了我很久,反复试验,不管path相对路径还是什么,都不能顺利实现。
后来我研究了jdk自带的demo,发现demo在引用资源的时候,采用这样的方法 getClass().getResource(Sing sourceName);
getClass()是Object的方法,返回一个对象的运行时类型,即CLass对象。
原来Class对象有getResource方法,在API文档中就是这样写的:
public URL getResource(Sing 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 bootsap 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
如法炮制,我把原来的
private ImageIcon logoButtonIcon = new ImageIcon(path);
改成
private ImageIcon logoButtonIcon = new ImageIcon(getClass().getResource(path));
编译,jar,run,成功,无论是本机打开还是放到http服务器中,都没有问题了。
这就是在Applet中引用jar中资源文件的KEY!
转载于:http://www.okxue8.com/article/2008/0725/article_3196.html
- JAVA Applet教程-如何在Applet中引用jar中的资源文件 作者:admin
- applet引用jar中的dll文件
- 在Applet中引用jar中图片等等
- 在Applet中如何读文件?
- 在Applet中引用jar中资源文件的KEY!
- 关于java applet引用第三方jar文件 applet权限的提升 通过网络访问
- 如何在HTML中加入Java Applet
- 在applet中访问本地资源
- 在applet中访问本地资源
- 在Java applet中如何显示另外一个HTML页面
- 在Java applet中如何实现一个模式对话框?
- 在Java applet中如何显示另外一个HTML页面?
- Web_如何在页面中插入java applet?
- 在Java Applet中的动画编程【转载】
- Java applet 在JSP中的使用
- 在Applet中读取文件内容
- 在Applet中读取文件内容
- 在Applet中读取服务器文件内容
- 系统程序员成长计划-你的数据放在哪里(下)
- [转]C++,VC++,MFC,ATL,WTL到底是什么关系么
- 写给大四即将找工作的同学
- 10月24日3G手机(Android/iPhone)高级技术讲座(北京)
- [转]CMS Content Management System(内容管理系统) 提供商
- JAVA Applet教程-如何在Applet中引用jar中的资源文件 作者:admin
- Tab页界面之二,用jQuery技术实现
- 试试再说(9.23)
- 线性查找和折半查找
- 小小标记
- C++编程思想(卷二):异常处理
- first
- 转贴【笑话】
- 浙江大学校长杨卫院士谈大学导师“十大戒”(1)