标签:键盘操作、小程序查看器
//<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;
}
public boolean keyDown(Event e,int key)//键盘操作,左右、上下移动
{
switch(key)
{
case Event.UP:
if(y>10) y-=10;
break;
case Event.DOWN:
if(y<190) y+=10;
break;
case Event.LEFT:
if(x>10) x-=10;
break;
case Event.RIGHT:
if(x<190) x+=10;
break;
default:
}
repaint();
return true;
}
}运行效果:
本文出自 “桑海田 博客专栏” 博客,请务必保留此出处http://10602803.blog.51cto.com/10592803/1683074
标签:键盘操作、小程序查看器
原文地址:http://10602803.blog.51cto.com/10592803/1683074