J2ME的ITEM类

一、基本知识
1、Item类是Form类的派生类。
2、通过改变Item类的派生类的实例的状态,用户可以和应用程序进行交互。
3、itemStateChanged方法和普通触发器不同,在用户引起状态变化时自动调用的操作,程序本身引起的不会调用。
二、创建实践

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

2、实际运行效果图

3、NETBEANS设计器的设计

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值