你對J2ME中ITEM類用法是否熟悉,這裡和大家簡單分享一下,為了便於大家理解通過圖裡向大家解釋,相信本文介紹一定會讓你有所收獲。
J2ME中ITEM類用法
一、基本知識
1、ITEM類是Form類的派生類。
2、通過改變ITEM類的派生類的實例的狀態,用戶可以和應用程序進行交互。
3、ITEM類StateChanged方法和普通觸發器不同,在用戶引起狀態變化時自動調用的操作,程序本身引起的不會調用。
二、創建實踐
1、以ChoiceGroup的應用為例,所有應用ITEM類的MIDlet如果要處理ITEM類的狀態變化必須重寫ITEM類StateChanged方法
2、實際運行效果圖
3、NETBEANS設計器的設計
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){
- }
- }