zl程序教程

您现在的位置是:首页 >  工具

当前栏目

Applet小应用程序开发简介

应用 简介 程序开发 Applet
2023-06-13 09:14:35 时间
第一个Applet小应用程序:
1.java文件代码

复制代码代码如下:

importjava.awt.*;
importjava.applet.Applet;
publicclassHelloWorldextendsApplet{
Stringtext;
puvlicvoidinit(){
text="HelloWorld";
//this指的是所在类(HelloWorld)的对象,但是H继承了Applet,Applet继承了JPanel类,JPanel类继承了Component。所以可以用this调用setBackground方法。
this.setBackground(newColor(120,180,140));
}
publicvoidpaint(Graphicsg){
g.drawString(text,25,25);
}
}

2.html文件代码
复制代码代码如下:

<html>
<appletcode="HelloWorld.class"width=200height=150></applet>
</html>

Applet对象具备GUI容器的性质,其默认布局管理器为FlowLayout类型。
Applet类提供了小应用程序及其运行环境间的标准接口,相关方法包括:
//初始态
publicvoidinit();
//运行态
publicvoidstart();
//停止态
publicvoidstop();
//消亡态
publicvoiddestroy();

//init()函数应用实例
复制代码代码如下:
importjava.awt.*;
importjava.applet.Applet;
publicclasstestextendsApplet{
publicvoidinit(){
this.add(newButton("Start"));
this.add(newButton("Stop"));
this.setBackground(Color.BLUE);
}
publicvoidpaint(Graphicsg){
g.drawString("HelloWorld!",20,20);
}
}

Applet的安全性限制
1、不能执行本地的可执行程序
2、除了所在html的服务器之外,不能在其他的远程服务器运行
3、不允许读写、删除本地计算机文件
4、只允许获得非敏感的信息

Applet插件标记
复制代码代码如下:
<applet
[archive=archiveList]
code=appletFile.class
width=pixels
height=pixels
[codebase=codebaseURL]//
[alt=alternateText]//在不支持或禁用java的浏览器中,在指定位置显示提示信息
[name=appletInstanceName]//可用作参数传递的标志
[align=alignment]//位置
[vspace=pixels]
[hspace=pixels]
>
[<paramname=val1value=value1/>]
[<paramname=val2value=value2/>]
</applet>

Applet插件应用

1、html文件代码
复制代码代码如下:
<html>
<appletcode="Test.class"width=200height=100>
<paramname="topic"value="ComputerScience"/>
</applet>
</html>

2、java文件代码
复制代码代码如下:
importjava.awt.*;
importjava.applet.Applet;

publicclassTestextendsApplet{
privateStringtopic;
publicvoidinit(){
topic=this.getParammeter("topic");
this.setBackground(newColor(120,180,140));
}
publicvoidpaint(Graphicsg){
g.drawString(topic,25,25);
}
}