[J2ME] 基本框架框架
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 stub 19 ds=Display.getDisplay(this);//创建Display类一个实体 20 exitCom=new Command("Exit",Command.EXIT,1);//创建Command对象一个实体,并设置Exit命令用于退出这个MIDlet 21 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 stub 28 29 } 30 31 //当系统要求MIDlet暂停时调用 32 protected void pauseApp() { 33 // TODO Auto-generated method stub 34 35 } 36 37 //第一次启动或者暂停后重启时由系统调用startApp()方法 38 protected void startApp() throws MIDletStateChangeException { 39 // TODO Auto-generated method stub 40 ds.setCurrent(tb);//将构造函数中的TextBox设置为当前的屏幕 41 } 42 43 //用户触发任何Command是,做出回应,系统会自动调用这个方法 44 public void commandAction(Command arg0, Displayable arg1) { 45 // TODO Auto-generated method stub 46 if(arg0==exitCom){ 47 try { 48 destroyApp(false); 49 notifyDestroyed(); 50 } catch (MIDletStateChangeException e) { 51 // TODO Auto-generated catch block 52 e.printStackTrace(); 53 } 54 } 55 } 56 57 }