溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

J2ME中ITEM類如何使用

發(fā)布時間:2021-06-23 15:03:57 來源:億速云 閱讀:118 作者:Leah 欄目:web開發(fā)

這篇文章給大家介紹J2ME中ITEM類如何使用,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

J2ME中ITEM類用法

一、基本知識

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

2、通過改變ITEM類的派生類的實例的狀態(tài),用戶可以和應(yīng)用程序進行交互。

3、ITEM類StateChanged方法和普通觸發(fā)器不同,在用戶引起狀態(tài)變化時自動調(diào)用的操作,程序本身引起的不會調(diào)用。

二、創(chuàng)建實踐

1、以ChoiceGroup的應(yīng)用為例,所有應(yīng)用ITEM類的MIDlet如果要處理ITEM類的狀態(tài)變化必須重寫ITEM類StateChanged方法

2、實際運行效果圖

J2ME中ITEM類如何使用

3、NETBEANS設(shè)計器的設(shè)計

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類的監(jiān)聽器  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){  }  }

關(guān)于J2ME中ITEM類如何使用就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI