import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class C1 extends MIDlet implements CommandListener {
// 实现CommandListener接口
// 继承MIDlet抽象类并覆盖方法
// 高级界面类Screen类继承了Displayable类
// TextBox,List,Alert,Form是Screen的子类
TextBox x = new TextBox("", "Hello JAVA!", 100, TextField.ANY);
Command exit = new Command("Exit", Command.EXIT, 8);
// 依次为,按键名称,按键类型,优先级。后面两个一般没有什么讲究,除非为了软件美观
public C1() {
// TODO Auto-generated constructor stub
}
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
Display display = Display.getDisplay(this);
// 单例模式,获得唯一的Display实例
// Display对象是控制这个画面的显示管理器
x.addCommand(exit);// addCommand方法
x.setCommandListener(this);// 设置一个监听器,没有这个监听器,按下指定键值是没有反应的
display.setCurrent(x);
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
// 实现CommandListener接口必须重写commandAction方法
public void commandAction(Command arg0, Displayable arg1) {
// TODO Auto-generated method stub
// 高级事件处理
if (arg0 == exit) {
//如果按下的键为exit,执行下列语句
System.out.println("退出啦!");// 在控制台中打印
notifyDestroyed();// 通知软件退出
}
}
}
