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

J2ME

开发平台:

Java

  1. import javax.microedition.lcdui.*;
  2. import javax.microedition.midlet.MIDlet;
  3. public class EXCLUSIVEItemStateListenerDemo
  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 EXCLUSIVEItemStateListenerDemo() {
  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.EXCLUSIVE,stringArray,imageArray);
  23.        mainForm.append(mChoiceGroup);
  24.        
  25.        mStringItem = new StringItem("您选择的是","——");
  26.        mainForm.append(mStringItem);
  27.        ItemStateListener listener = new ItemStateListener(){
  28.        public void itemStateChanged(Item item){
  29.        if(item == mChoiceGroup){
  30.        boolean[] selectedArray = new boolean[mChoiceGroup.size()];
  31.        mChoiceGroup.getSelectedFlags(selectedArray);
  32.        StringBuffer selectedString = new StringBuffer();
  33.        for(int i=0;i<selectedArray.length;i++){
  34.        if(selectedArray[i]){
  35.        selectedString.append("").append(new StringBuffer(mChoiceGroup.getString(i)));
  36.        }
  37.        }
  38.        mStringItem.setText(selectedString.toString());
  39.        }
  40.        }
  41.        };
  42.        mainForm.setItemStateListener(listener); 
  43.        
  44.        
  45.        mainForm.addCommand(CMD_EXIT);
  46.        mainForm.setCommandListener(this);
  47.     }
  48.     protected void startApp() {        
  49.         display.setCurrent(mainForm);
  50.     }
  51.     public void commandAction(Command c, Displayable d) {
  52.         if (c == CMD_EXIT) {
  53.             destroyApp(false);
  54.             notifyDestroyed();
  55.         }
  56.     }
  57.     protected void destroyApp(boolean unconditional) {
  58.     }
  59.     protected void pauseApp() {
  60.     }
  61. }