温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

J2ME中ITEM类如何使用

发布时间:2021-06-23 15:03:57 来源:亿速云 阅读:109 作者:Leah 栏目:web开发

这篇文章给大家介绍J2ME中ITEM类如何使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

J2ME中ITEM类用法

一、基本知识

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

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

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

二、创建实践

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

2、实际运行效果图

J2ME中ITEM类如何使用

3、NETBEANS设计器的设计

J2ME中ITEM类如何使用

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中ITEM类如何使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI