码迷,mamicode.com
首页 > 其他好文 > 详细

键盘操作、小程序查看器

时间:2015-08-09 19:04:13      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:键盘操作、小程序查看器

//<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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!