/*
* 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()是在畫布顯示在顯示屏之前系統將先調用該方法。