天天看点

J2ME的ITEM类

一、基本知识

1、Item类是Form类的派生类。

2、通过改变Item类的派生类的实例的状态,用户可以和应用程序进行交互。

3、itemStateChanged方法和普通触发器不同,在用户引起状态变化时自动调用的操作,程序本身引起的不会调用。

二、创建实践

1、以ChoiceGroup的应用为例,所有应用ITEM的MIDlet如果要处理ITEM的状态变化必须重写itemStateChanged方法

2、实际运行效果图

J2ME的ITEM类

3、NETBEANS设计器的设计

J2ME的ITEM类

4、代码(NETBEANS生成的大部分框架,笔者修改了其中几行,增加了itemStateChanged方法)

package hello;

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

public class HelloMIDlet extends MIDlet implementsCommandListener,ItemStateListener{

    privateboolean midletPaused = false;

    //

    privateCommand exitCommand;

    private Formform;

    privateChoiceGroup weather_CG;

    //

    publicHelloMIDlet() {

    }

    //

    //

    //

    private voidinitialize() {

       // write pre-initialize user code here

       // write post-initialize user code here

    }

    //

    //

    public voidstartMIDlet() {

       // write pre-action user code here

       switchDisplayable(null, getForm());

       // write post-action user code here

    }

    //

    //

    public voidresumeMIDlet() {

       // write pre-action user code here

       // write post-action user code here

    }

    //

    //

    public voidswitchDisplayable(Alert alert, Displayable nextDisplayable) {

       // write pre-switch user code here

       Display display = getDisplay();

       if (alert == null) {

           display.setCurrent(nextDisplayable);

       } else {

           display.setCurrent(alert, nextDisplayable);

       }

       // write post-switch user code here

    }

    //

    //

    public voidcommandAction(Command command, Displayable displayable) {

       // write pre-action user code here

       if (displayable == form) {

           if (command == exitCommand) {

               // write pre-action user code here

               exitMIDlet();

               // write post-action user code here

           }

       }

       // write post-action user code here

    }

    //

 //重写ItemStateChanged方法

    public voiditemStateChanged(Item item) {

       // write pre-action user code here

       if (item == weather_CG) {

              form.setTitle("你选择了"+weather_CG.getString(weather_CG.getSelectedIndex())+"天");

       // write post-action user codehere          

       }

       // write post-action user code here

   }

   //

    //

    publicCommand getExitCommand() {

       if (exitCommand == null) {

           // write pre-init user code here

           exitCommand = new Command("/u9000/u51FA", Command.EXIT, 0);

           // write post-init user code here

       }

       return exitCommand;

    }

    //

    //

    public FormgetForm() {

       if (form == null) {

           // write pre-init user code here

           form = new Form("Welcome", new Item[] { getWeather_CG() });

           form.addCommand(getExitCommand());

           form.setCommandListener(this);

           //增加初始天气选择情况显示

           form.setTitle("你选择了晴天");      

           //增加ITEM的监听器

           form.setItemStateListener(this);           // write post-init user codehere

       }

       return form;

    }

    //

    //

    publicChoiceGroup getWeather_CG() {

       if (weather_CG == null) {

           // write pre-init user code here

           weather_CG = new ChoiceGroup("/u5929/u6C14/u7C7B/u578B",Choice.EXCLUSIVE);

           weather_CG.setLayout(ImageItem.LAYOUT_DEFAULT);

           weather_CG.setFitPolicy(Choice.TEXT_WRAP_DEFAULT);

           //选项框项的代码

           weather_CG.append("晴",null);

           weather_CG.append("阴",null);          

           weather_CG.append("雨",null);

           weather_CG.append("雪",null);

           weather_CG.setSelectedIndex(0,true);

           // write post-init user code here

       }

       return weather_CG;

    }

    //

    publicDisplay getDisplay () {

       return Display.getDisplay(this);

    }

    public voidexitMIDlet() {

       switchDisplayable (null, null);

       destroyApp(true);

       notifyDestroyed();

    }

    public voidstartApp() {

       if (midletPaused) {

           resumeMIDlet ();

       } else {

           initialize ();

           startMIDlet ();

       }

       midletPaused = false;

    }

    public voidpauseApp() {

       midletPaused = true;

    }

    public voiddestroyApp(boolean unconditional) {

    }

}