分类:
2008-09-09 16:48:00
private StringBuffer sb=new StringBuffer(); //定义一个StringBuffer用来保存输入的数字
private int len=4; //想输入的数字的长度
private int temp; //取数字
protected void paint(Graphics g)
{
g.setColor(WHITE);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(BLACK);
g.drawString(sb.toString(),getWidth()/2,getHeight()/2,Graphics.TOP|Graphics.HCENTER);
//显示输入内容
}
protected void keyPressed(int keynum)
{
if (sb.length()==len) //开始键功能描述
{
switch (keynum)
{
case KEY_STAR:
temp=Integer.parseInt(sb.toString()); //得到了屏幕中输入的数字 接下来自己可以利用数字按下*开始动能
break;
default:
break;
}
}
if (sb.length() {
switch (keynum)
{
case KEY_NUM1:
sb.append("1");
break;
case KEY_NUM2:
sb.append("2");
break;
case KEY_NUM3:
sb.append("3");
break;
case KEY_NUM4:
sb.append("4");
break;
case KEY_NUM5:
sb.append("5");
break;
case KEY_NUM6:
sb.append("6");
break;
case KEY_NUM7:
sb.append("7");
break;
case KEY_NUM8:
sb.append("8");
break;
case KEY_NUM9:
sb.append("9");
break;
case KEY_NUM0:
sb.append("0");
break;
default: break;
}
}
if(sb.length()>0) //退格键函数描述
{
if(keynum==KEY_POUND) sb.deleteCharAt(sb.length()-1);
}
repaint();
}
protected void keyReapeated(int keynum)
{
if(sb.length()>0) //长按一次性去除建函数描述
if(keynum==KEY_POUND) sb=null; }
repaint();
}