標籤:des style blog http java color io 2014
1 import javax.microedition.lcdui.Command; 2 import javax.microedition.lcdui.CommandListener; 3 import javax.microedition.lcdui.Display; 4 import javax.microedition.lcdui.Displayable; 5 import javax.microedition.lcdui.TextBox; 6 import javax.microedition.midlet.MIDlet; 7 import javax.microedition.midlet.MIDletStateChangeException; 8 9 10 public class main extends MIDlet implements CommandListener {11 12 private Command exitCom;13 private Display ds;14 private TextBox tb;15 16 //建構函式17 public main() {18 // TODO Auto-generated constructor stub19 ds=Display.getDisplay(this);//建立Display類一個實體20 exitCom=new Command("Exit",Command.EXIT,1);//建立Command對象一個實體,並設定Exit命令用於退出這個MIDlet21 tb=new TextBox("Hellow MIDlet","Hellow World",15,0);//建立用來輸出東西的TextBox對象22 tb.addCommand(exitCom);//使Command對象和TextBox關聯起來23 tb.setCommandListener(this);//當TextBox顯示在螢幕上時,使CommandListener響應發生的事件24 }25 26 protected void destroyApp(boolean arg0) throws MIDletStateChangeException {27 // TODO Auto-generated method stub28 29 }30 31 //當系統要求MIDlet暫停時調用32 protected void pauseApp() {33 // TODO Auto-generated method stub34 35 }36 37 //第一次啟動或者暫停後重啟時由系統調用startApp()方法38 protected void startApp() throws MIDletStateChangeException {39 // TODO Auto-generated method stub40 ds.setCurrent(tb);//將建構函式中的TextBox設定為當前的螢幕41 }42 43 //使用者觸發任何Command是,做出回應,系統會自動調用這個方法44 public void commandAction(Command arg0, Displayable arg1) {45 // TODO Auto-generated method stub46 if(arg0==exitCom){47 try {48 destroyApp(false);49 notifyDestroyed();50 } catch (MIDletStateChangeException e) {51 // TODO Auto-generated catch block52 e.printStackTrace();53 }54 }55 }56 57 }