J2ME ChoiceGroup 组件测试
程序员文章站
2022-05-15 21:20:36
...
import javax.microedition.lcdui.ChoiceGroup; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.Image; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; //ChoiceGroup 组件测试 public class ChoiceGroup_test extends MIDlet implements CommandListener { // 定义设置变量 private Display display; private Form f; private Image image; private Image[] imageArray; private ChoiceGroup choice; private ChoiceGroup choice2; private String[] stringArray1 = {"宫爆鸡丁","回锅肉","西红柿"}; private String[] stringArray2 = {"11:30-12:00","12:00-12:30","12:30-13:00"}; private final Command CMD_EXIT = new Command("eixt",Command.EXIT,1); private final Command CMD_OK = new Command("OK",Command.OK,1); //构造方法的编写 public ChoiceGroup_test(){ super(); //获取当前的对象 display = Display.getDisplay(this); } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { // TODO Auto-generated method stub } protected void pauseApp() { // TODO Auto-generated method stub display.setCurrent(null); f = null; } protected void startApp() throws MIDletStateChangeException { // TODO Auto-generated method stub //同样ChoiceGroup 这个组件是Form 的子类 f = new Form("Choice 的演示"); f.append("订餐表"); try{ //获取对应的图片 使用的是相对的路径 Image image = Image.createImage("/images/eclipse.png"); //给之前定义好的数组进行赋值 imageArray = new Image[]{image,image,image}; //创建组件 choice = new ChoiceGroup("选出你的午餐菜谱:",ChoiceGroup.MULTIPLE,stringArray1,imageArray); choice2 = new ChoiceGroup("选择送餐的时间:",ChoiceGroup.EXCLUSIVE,stringArray2,null); //将对应的组件添加到Form的对象中 f.append(choice); f.append(choice2); }catch(Exception ex){ System.out.println("我是监控异常的!无敌一号!"); } //添加命令 f.addCommand(CMD_EXIT); f.addCommand(CMD_OK); f.setCommandListener(this); //屏幕显示交互 display.setCurrent(f); } public void commandAction(Command c, Displayable d) { // TODO Auto-generated method stub // 执行相应的命令 传递对应的组件对象 //如果选择了退出 销毁资源 if(c==CMD_EXIT){ try { destroyApp(false); } catch (MIDletStateChangeException e) { // TODO Auto-generated catch block e.printStackTrace(); } notifyDestroyed(); } //选择了确定 多选模式, 传入的是个集合 if(c==CMD_OK){ String content = "你选择的午餐有:"; for(int i = 0;i<choice.size();i++){ if(choice.isSelected(i)){ content+=stringArray1[i]; //累加选择的 } } String contentTime = "送餐时间为"+stringArray2[choice2.getSelectedIndex()]; f.deleteAll(); f.append(content); f.append(contentTime); } } }
推荐阅读