【Java开源代码栏目提醒】:本文主要为网学会员提供CommandEvent_MIDlet.java,希望对需要CommandEvent_MIDlet.java网友有所帮助,学习一下!
package ch04;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class CommandEvent_MIDlet
extends MIDlet
implements CommandListener {
//声明一个Display对象
Display display;
//声明一个List列表对象
List menu;
//声明三个代表图标的对象
Image aimage, bimage, cimage;
//创建两个按钮
Command backCommand = new Command("返回", Command.BACK, 0);
Command exitCommand = new Command("退出", Command.STOP, 2);
/*
1.构造器
*/
public CommandEvent_MIDlet() {
display = Display.getDisplay(this);
try {
aimage = Image.createImage("/icons/background/star.png");
bimage = Image.createImage("/icons/background/hand.png");
cimage = Image.createImage("/icons/background/boot.png");
}
catch (Exception e) {
System.out.println(e.getMessage());
}
menu = new List("菜单", Choice.IMPLICIT);
menu.append("选项1", aimage);
menu.append("选项2", bimage);
menu.append("选项3", cimage);
display.setCurrent(menu);
menu.addCommand(exitCommand);
menu.setCommandListener(this);
}
/*
2.响应按钮事件
*/
public void commandAction(Command c, Displayable d) {
String label = c.getLabel();
if (label.equals("退出")) {
destroyApp(true);
}
else if (label.equals("返回")) {
display.setCurrent(menu);
}
else {
List down = (List) display.getCurrent();
switch (down.getSelectedIndex()) {
case 0:
prepare("选项1");
break;
case 1:
prepare("选项2");
break;
case 2:
prepare("选项3");
break;
}
}
}
/*
3.返回提示信息
*/
public void prepare(String sn) {
Alert currentAlert = new Alert("用户选择提示信息:");
currentAlert.setString("提示:您选择了" + sn + "!");
currentAlert.setTimeout(Alert.FOREVER);
currentAlert.addCommand(backCommand);
currentAlert.setCommandListener(this);
display.setCurrent(currentAlert);
}
//启动应用程序
public void startApp() throws MIDletStateChangeException {
}
//挂起应用程序
public void pauseApp() {
display = null;
menu = null;
}
//撤销应用程序
public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
}