你对J2ME中ITEM类用法是否熟悉,这里和大家简单分享一下,为了便于大家理解通过图里向大家解释,相信本文介绍一定会让你有所收获。
J2ME中ITEM类用法
一、基本知识
1、ITEM类是Form类的派生类。
2、通过改变ITEM类的派生类的实例的状态,用户可以和应用程序进行交互。
3、ITEM类StateChanged方法和普通触发器不同,在用户引起状态变化时自动调用的操作,程序本身引起的不会调用。
二、创建实践
1、以ChoiceGroup的应用为例,所有应用ITEM类的MIDlet如果要处理ITEM类的状态变化必须重写ITEM类StateChanged方法
2、实际运行效果图
3、NETBEANS设计器的设计
#p#
4、代码(NETBEANS生成的大部分框架,笔者修改了其中几行,增加了ITEM类StateChanged方法)
- packagehello;
- importjavax.microedition.midlet.*;
- importjavax.microedition.lcdui.*;
- publicclassHelloMIDletextendsMIDletimplementsCommandListener,
- ITEM类StateListener{
- privatebooleanmidletPaused=false;
- //
- privateCommandexitCommand;
- privateFormform;
- privateChoiceGroupweather_CG;
- //
- publicHelloMIDlet(){
- }
- //
- //
- //
- privatevoidinitialize(){
- //writepre-initializeusercodehere
- //writepost-initializeusercodehere
- }
- //
- //
- publicvoidstartMIDlet(){
- //writepre-actionusercodehere
- switchDisplayable(null,getForm());
- //writepost-actionusercodehere
- }
- //
- //
- publicvoidresumeMIDlet(){
- //writepre-actionusercodehere
- //writepost-actionusercodehere
- }
- //
- //
- publicvoidswitchDisplayable(Alertalert,
- DisplayablenextDisplayable){
- //writepre-switchusercodehere
- Displaydisplay=getDisplay();
- if(alert==null){
- display.setCurrent(nextDisplayable);
- }else{
- display.setCurrent(alert,nextDisplayable);
- }
- //writepost-switchusercodehere
- }
- //
- //
- publicvoidcommandAction(Commandcommand,
- Displayabledisplayable){
- //writepre-actionusercodehere
- if(displayable==form){
- if(command==exitCommand){
- //writepre-actionusercodehere
- exitMIDlet();
- //writepost-actionusercodehere
- }
- }
- //writepost-actionusercodehere
- }
- //
- //重写ITEM类StateChanged方法
- publicvoidITEM类StateChanged(ITEM类ITEM类){
- //writepre-actionusercodehere
- if(ITEM类==weather_CG){
- form.setTitle("你选择了"+weather_CG.getString
- (weather_CG.getSelectedIndex())+"天");
- //writepost-actionusercodehere
- }
- //writepost-actionusercodehere
- }
- //
- //
- publicCommandgetExitCommand(){
- if(exitCommand==null){
- //writepre-initusercodehere
- exitCommand=newCommand("\u9000\u51FA",Command.EXIT,0);
- //writepost-initusercodehere
- }
- returnexitCommand;
- }
- //
- //
- publicFormgetForm(){
- if(form==null){
- //writepre-initusercodehere
- form=newForm("Welcome",newITEM类[]{getWeather_CG()});
- form.addCommand(getExitCommand());
- form.setCommandListener(this);
- //增加初始天气选择情况显示
- form.setTitle("你选择了晴天");
- //增加ITEM类的监听器
- form.setITEM类StateListener(this);
- //writepost-initusercodehere
- }
- returnform;
- }
- //
- //
- publicChoiceGroupgetWeather_CG(){
- if(weather_CG==null){
- //writepre-initusercodehere
- weather_CG=newChoiceGroup
- ("\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);
- //writepost-initusercodehere
- }
- returnweather_CG;
- }
- //
- publicDisplaygetDisplay(){
- returnDisplay.getDisplay(this);
- }
- publicvoidexitMIDlet(){
- switchDisplayable(null,null);
- destroyApp(true);
- notifyDestroyed();
- }
- publicvoidstartApp(){
- if(midletPaused){
- resumeMIDlet();
- }else{
- initialize();
- startMIDlet();
- }
- midletPaused=false;
- }
- publicvoidpauseApp(){
- midletPaused=true;
- }
- publicvoiddestroyApp(booleanunconditional){
- }
- }
【编辑推荐】
- 深入探究J2ME Hashtable实现原理
- J2ME中的Display类的两大作用
- J2ME数据结构中Hashtable和Vector的使用
- MotorolaJ2ME开发时需要注意的几个细节
- Java2平台J2SE、J2EE、J2ME三大版本的区别