编写并运行一个MIDlet
MIDPlet简介
MIDP中定义的应用程序称为MIDlet。任何一个MIDlet都是javax.microedition.midlet.MIDlet的子类,必须继承自javax.microedition.midlet.MIDlet。这很显而易见。我们在J2SE中编过Applet,Applet就必须继承自 java.applet.Applet。是不是很类似。请看下图,说明了MIDlet的继承体系。

图4 MIDlet的继承体系
一个简单的MIDlet
//HelloWorld.java,一个最简单的MIDlet程序。
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class HelloWorld extends MIDlet implements CommandListener {
private Command exitCommand;
private TextBox tb;
public HelloWorld(){
exitCommand =new Command("Exit",Command.EXIT,1);
tb =new TextBox("Hello MIDlet","Hello,World!",15,0);
tb.addCommand(exitCommand);
tb.setCommandListener(this);
}
protected void startApp(){
Display.getDisplay(this).setCurrent(tb);
}
protected void pauseApp(){
}
protected void destroyApp(boolean u){
}
public void commandAction(Command c,Displayable d){
if (c ==exitCommand){
destroyApp(false);
notifyDestroyed();
}
}
}
该程序显示“HelloWorld”字符串。程序本身非常简单,如果你熟悉JavaApplet编程的话,你会发现与Applet很类似。好,我们先把它编译,运行一下看看。
