POPUPItemStateListenerDemo.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:2k
源码类别:

J2ME

开发平台:

Java

  1. import javax.microedition.lcdui.*;
  2. import javax.microedition.midlet.MIDlet;
  3. public class POPUPItemStateListenerDemo
  4.     extends MIDlet implements CommandListener {
  5.     private final static Command CMD_EXIT = new Command("Exit", Command.EXIT, 1);
  6.     private Display display;
  7.     private ChoiceGroup mChoiceGroup;
  8.     private Form mainForm;
  9.     private StringItem mStringItem;
  10.     public POPUPItemStateListenerDemo() {
  11.        display = Display.getDisplay(this);
  12.        mainForm = new Form("Choice Group演示");
  13.        Image[] imageArray = null;
  14.        try {
  15.                 Image star = Image.createImage("/star.png");
  16.                 imageArray = new Image[] { star, star, star, star, star, star};
  17.        } catch (java.io.IOException err) {
  18.        }
  19.        String[] stringArray = {
  20.                 "红心大战","空当接龙","三维弹球","蜘蛛纸牌","翻转棋","扫雷"
  21.       };
  22.        mChoiceGroup = new ChoiceGroup("选择游戏项目",ChoiceGroup.POPUP,stringArray,imageArray);
  23.        mainForm.append(mChoiceGroup);
  24.        
  25.        /*mainForm.append(
  26.        new ChoiceGroup("选择游戏项目",
  27.                        ChoiceGroup.POPUP,
  28.                        new String[]{"红心大战","空当接龙","三维弹球","蜘蛛纸牌","翻转棋","扫雷"},
  29.                        null));*/
  30.        mainForm.addCommand(CMD_EXIT);
  31.        mainForm.setCommandListener(this);
  32.        mStringItem = new StringItem("您选择的是","——");
  33.        mainForm.append(mStringItem);
  34.        ItemStateListener listener = new ItemStateListener(){
  35.        public void itemStateChanged(Item item){
  36.        if(item == mChoiceGroup){
  37.        boolean[] selectedArray = new boolean[mChoiceGroup.size()];
  38.        mChoiceGroup.getSelectedFlags(selectedArray);
  39.        StringBuffer selectedString = new StringBuffer();
  40.        for(int i=0;i<selectedArray.length;i++){
  41.        if(selectedArray[i]){
  42.        selectedString.append(" ").append(new StringBuffer(mChoiceGroup.getString(i)));
  43.        }
  44.        }
  45.        mStringItem.setText(selectedString.toString());
  46.        }
  47.        }
  48.        };
  49.        mainForm.setItemStateListener(listener); 
  50.     }
  51.     protected void startApp() {        
  52.         display.setCurrent(mainForm);
  53.     }
  54.     public void commandAction(Command c, Displayable d) {
  55.         if (c == CMD_EXIT) {
  56.             destroyApp(false);
  57.             notifyDestroyed();
  58.         }
  59.     }
  60.     protected void destroyApp(boolean unconditional) {
  61.     }
  62.     protected void pauseApp() {
  63.     }
  64. }