/*
* canvas3let.java
*
* created on 2005年4月20日, 下午3:55
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
*
* @author administrator
* @version
*/
public class canvas3let extends midlet implements commandlistener{
private display adisplay;
private command okcommand;
private command exitcommand;
private command backcommand;
private textbox atextbox;
private alert showalert;
private image showimage;
private mycanvas amycanvas;
public canvas3let(){
exitcommand=new command(“退出”,command.exit,1);
okcommand=new command(“进入”,command.ok,1);
try{
showimage=image.createimage(“/sohu.png”);
}
catch(exception e){}
showalert=new alert(“loading…”,”载入中……”,showimage,alerttype.info);
atextbox=new textbox(“keypress test”,
“按下进入键后,即可进入显示界面,可以显示所按按键”,
25,textfield.any);
atextbox.addcommand(exitcommand);
atextbox.addcommand(okcommand);
atextbox.setcommandlistener(this);
}
public void startapp() {
adisplay=display.getdisplay(this);
adisplay.setcurrent(atextbox);
}
public void pauseapp() {
}
public void destroyapp(boolean unconditional) {
}
public void commandaction(command c , displayable d){
if(c==exitcommand){
destroyapp(false);
notifydestroyed();
}
else if (c==okcommand){
amycanvas=new mycanvas();
adisplay.setcurrent(amycanvas);
}
}
public class mycanvas extends canvas implements commandlistener{
string keyname=””;
public mycanvas(){
backcommand=new command(“后退”,command.exit,1);
addcommand(backcommand);
setcommandlistener(this);
}
public void paint(graphics g){
//清除屏幕
g.setcolor(0xffffff);
g.fillrect(0,0,getwidth(),getheight());
//设置字体颜色并画出所按键的名称
g.setcolor(0);
g.drawstring(keyname,100,100,graphics.left|graphics.bottom);
}
public void commandaction(command c , displayable d){
if (c==backcommand){
hidenotify();
//adisplay.setcurrent(atextbox);
}
}
public void keypressed(int keycode){
keyname=getkeyname(keycode);
repaint();
}
protected void hidenotify(){
adisplay.setcurrent(showalert,atextbox);
}
}
}
通过这个程序,主要练习了keypress的用法。此外,还有hidenotify()的用法,但shownotify()的用法还不是很清楚。
hidenotify()是在画布被从显示屏移除后,实现立刻调用该方法。
shonotify()是在画布显示在显示屏之前系统将先调用该方法。