📄 ui_ex23.java
字号:
// 程序名UI_Ex23.java
// 测试ChoiceGroup对象的事件
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class UI_Ex23 extends MIDlet implements CommandListener {
private Display display; // 引用MIDlet的Display 对象
private Form form; // 声明一个Form对象
private Command cmdExit; // 设定按钮用于退出MIDlet
private ChoiceGroup choiceGroup; //设置一个ChoiceGroup对象
public UI_Ex23() {
display = Display.getDisplay(this);// 获取一个Display实例,它是唯一的
choiceGroup = new ChoiceGroup("Choose direction:",ChoiceGroup.EXCLUSIVE);// 构造一个ChoiceGroup对象
choiceGroup.append("East",null);
choiceGroup.append("West",null);
choiceGroup.append("South",null);
choiceGroup.append("North",null);
form = new Form("Test ChoiceGroup");
form.append(choiceGroup);
form.append(new StringItem("What is selected:","________"));
cmdExit = new Command("Exit", Command.SCREEN, 1);
form.addCommand(cmdExit);// 为Displayable对象添加一个Command对象
form.setCommandListener(this);// 为Displayable对象添加事件监听器
ItemStateListener listener = new ItemStateListener() {
public void itemStateChanged(Item item) {
if (item == choiceGroup) {
boolean[] selectedArray = new boolean[choiceGroup.size()];
choiceGroup.getSelectedFlags(selectedArray);
StringBuffer selectedString = new StringBuffer();
for (int i = 0;i<selectedArray.length;i++) {
if (selectedArray[i]) {
selectedString.append(" ").append(new StringBuffer(choiceGroup.getString(i)));
}
}
((StringItem)form.get(1)).setText(selectedString.toString());
}
}
};
form.setItemStateListener(listener);
}
// 被应用程序管理器调用来启动MIDlet。
public void startApp() {
display.setCurrent(form);// 把form设置为当前屏幕或当前Displayable对象
}
// 一个必要的方法
public void pauseApp() {
}
// 一个必要的方法
public void destroyApp(boolean unconditional) {
}
// 设置事件触发时的动作
public void commandAction(Command c, Displayable d) {
if (c == cmdExit) {
destroyApp(false);
notifyDestroyed();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -