天天看點

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) {

    }

}

繼續閱讀