程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2ME >> J2ME中ITEM類用法實例解析

J2ME中ITEM類用法實例解析

編輯:J2ME

你對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方法)

  1. packagehello;  
  2. importJavax.microedition.midlet.*;  
  3. importJavax.microedition.lcdui.*;  
  4.  
  5. publicclassHelloMIDletextendsMIDletimplementsCommandListener,
  6. ITEM類StateListener{  
  7. privatebooleanmidletPaused=false;  
  8. //  
  9. privateCommandexitCommand;  
  10. privateFormform;  
  11. privateChoiceGroupweather_CG;  
  12. //  
  13.  
  14. publicHelloMIDlet(){  
  15. }  
  16. //  
  17. //  
  18. //  
  19.  
  20. privatevoidinitialize(){  
  21. //writepre-initializeusercodehere  
  22.  
  23. //writepost-initializeusercodehere  
  24. }  
  25. //  
  26. //  
  27.  
  28. publicvoidstartMIDlet(){  
  29. //writepre-actionusercodehere  
  30. switchDisplayable(null,getForm());  
  31. //writepost-actionusercodehere  
  32. }  
  33. //  
  34. //  
  35. publicvoidresumeMIDlet(){  
  36. //writepre-actionusercodehere  
  37.  
  38. //writepost-actionusercodehere  
  39. }  
  40. //  
  41. //  
  42.  
  43. publicvoidswitchDisplayable(Alertalert,
  44. DisplayablenextDisplayable){  
  45. //writepre-switchusercodehere  
  46. Displaydisplay=getDisplay();  
  47. if(alert==null){  
  48. display.setCurrent(nextDisplayable);  
  49. }else{  
  50. display.setCurrent(alert,nextDisplayable);  
  51. }  
  52. //writepost-switchusercodehere  
  53. }  
  54. //  
  55. //  
  56.  
  57. publicvoidcommandAction(Commandcommand,
  58. Displayabledisplayable){  
  59. //writepre-actionusercodehere  
  60. if(displayable==form){  
  61. if(command==exitCommand){  
  62. //writepre-actionusercodehere  
  63. exitMIDlet();  
  64. //writepost-actionusercodehere  
  65. }  
  66. }  
  67. //writepost-actionusercodehere  
  68. }  
  69. //  
  70. //重寫ITEM類StateChanged方法  
  71. publicvoidITEM類StateChanged(ITEM類ITEM類){  
  72. //writepre-actionusercodehere  
  73. if(ITEM類==weather_CG){  
  74. form.setTitle("你選擇了"+weather_CG.getString
  75. (weather_CG.getSelectedIndex())+"天");  
  76. //writepost-actionusercodehere  
  77. }  
  78. //writepost-actionusercodehere  
  79. }  
  80. //  
  81.  
  82. //  
  83.  
  84. publicCommandgetExitCommand(){  
  85. if(exitCommand==null){  
  86. //writepre-initusercodehere  
  87. exitCommand=newCommand("\u9000\u51FA",Command.EXIT,0);  
  88. //writepost-initusercodehere  
  89. }  
  90. returnexitCommand;  
  91. }  
  92. //  
  93. //  
  94. publicFormgetForm(){  
  95. if(form==null){  
  96. //writepre-initusercodehere  
  97. form=newForm("Welcome",newITEM類[]{getWeather_CG()});  
  98. form.addCommand(getExitCommand());  
  99. form.setCommandListener(this);  
  100. //增加初始天氣選擇情況顯示  
  101. form.setTitle("你選擇了晴天");  
  102. //增加ITEM類的監聽器  
  103. form.setITEM類StateListener(this);
  104. //writepost-initusercodehere  
  105. }  
  106. returnform;  
  107. }  
  108. //  
  109.  
  110. //  
  111.  
  112. publicChoiceGroupgetWeather_CG(){  
  113. if(weather_CG==null){  
  114. //writepre-initusercodehere  
  115. weather_CG=newChoiceGroup
  116. ("\u5929\u6C14\u7C7B\u578B",Choice.EXCLUSIVE);  
  117. weather_CG.setLayout(ImageITEM類.LAYOUT_DEFAULT);  
  118. weather_CG.setFitPolicy(Choice.TEXT_WRAP_DEFAULT);  
  119. //選項框項的代碼  
  120. weather_CG.append("晴",null);  
  121. weather_CG.append("陰",null);  
  122. weather_CG.append("雨",null);  
  123. weather_CG.append("雪",null);  
  124. weather_CG.setSelectedIndex(0,true);  
  125. //writepost-initusercodehere  
  126. }  
  127. returnweather_CG;  
  128. }  
  129. //  
  130.  
  131.  
  132.  
  133.  
  134. publicDisplaygetDisplay(){  
  135. returnDisplay.getDisplay(this);  
  136. }  
  137.  
  138. publicvoidexitMIDlet(){  
  139. switchDisplayable(null,null);  
  140. destroyApp(true);  
  141. notifyDestroyed();  
  142. }  
  143.  
  144. publicvoidstartApp(){  
  145. if(midletPaused){  
  146. resumeMIDlet();  
  147. }else{  
  148. initialize();  
  149. startMIDlet();  
  150. }  
  151. midletPaused=false;  
  152. }  
  153.  
  154. publicvoidpauseApp(){  
  155. midletPaused=true;  
  156. }  
  157.  
  158. publicvoiddestroyApp(booleanunconditional){  
  159. }  
  160. }  
  161.  
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved