Applet
Applet是什么?Applet(小应用程序)采用Java创建的基于HTML的程序。
浏览器现实网页的原理:
网页文件就是一个普通的文本文件
网页脚本编程的原理:
Applet是GUI的一个组件
public class MyApplet extends Applet{
public void paint(Graphcis g){
g.drawSwing("this is a simple applet",50,60);
}
}
<applet code=MyApplet width=300 heigth=300></applet>
Applet执行过程:
1、浏览器载入HTML文件
2、浏览器载入html文件中引用的applet的字节码
3、浏览器启动java虚拟机执行applet
appletviewer test.html
applet小应用程序通常需要覆盖的方法:
init() start() stop() destory() paint()
public class MyApplet extends Applet{
public void paint(Graphcis g){
g.drawString("this is a simple applet",50,60);
}
public init(){
addMouseListener(new MouseAdapter(){
int orgX;
int orgY;
public void mousePressed(MouseEvent e){
orgX=e.getX();
orgY=e.getY();
}
public void mouseReleased(MouseEvent e)
{
getGraphics.drawLine(orgX,orgY,e.getX(),e.getY());
}
}
});
}
}
现实动画的Applet小程序
public class MyApplet extends Applet implements Runnable
{
int index;
Image[] image=new Image[10];
public void init()
{
for(int i=0;i<10;i++)
{
image[i]=getToolkit().getImage("imgs\\T"+(i+1)+".gif");//不能用磁盘的绝对路径
}
new Thread(this).start();
}
public void paint(Graphics g)
{
g.drawImage(image[index],0,0, this);
g.setFont(new Font(null,Font.ITALIC|Font.BOLD, 30));
g.drawString(""+index, 0, 60);
}
public void run()
{
while (true)
{
try
{
Thread.sleep(100);
} catch (Exception e)
{
e.printStackTrace();
}
index=(index+1)%10;
repaint();
}
}
}
<body>
<center>
<applet code="MyApplet.class" width="500" height="300">
</applet>
</center>
</body>
applet程序的限制:
不能启动执行其他应用程序
不能进行文件I/O操作:
不能调试本机代码:
不能调用本机代码
不能与Applet所在的主机之外的其他主机进行网络连接
<OBJECT>标记