//<applet code=ButtonApplet.class width=400 height=300></applet>
import java.awt.*;import java.applet.*;
public class ButtonApplet extends Applet
{
int x,y;
Button Bt;
public ButtonApplet()//加入按鈕
{
x=y=100;
Bt=new Button("按我");
add("Center",Bt);
}
public void paint(Graphics g)
{
g.drawString("Faint",x,y);
}
public boolean action(Event ev,Object arg)//觸發Applet,可以點選按鈕,字元串換位置
{
if(ev.target instanceof Button)
{
if(x>=200)
x=0;
else
x+=20;
if(y<=200)
y=200;
else
y-=10;
repaint();
}
return true;
}
}
運作結果: