J2ME中ITEM类用法实例解析

开发 前端
本文向大家简单介绍一下J2ME中ITEM类用法,Item类是Form类的派生类,通过改变Item类的派生类的实例的状态,用户可以和应用程序进行交互。

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

  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. 深入探究J2ME Hashtable实现原理
  2. J2ME中的Display类的两大作用
  3. J2ME数据结构中Hashtable和Vector的使用
  4. MotorolaJ2ME开发时需要注意的几个细节
  5. Java2平台J2SE、J2EE、J2ME三大版本的区别

 

责任编辑:佚名 来源: blog.sina.com.cn
相关推荐

2010-09-30 12:53:00

J2MECSS

2010-09-29 08:57:04

J2ME前景

2010-09-29 13:23:12

J2MEPIM

2010-09-30 11:16:53

J2ME Snake脚

2010-09-29 13:50:31

J2MEJ2SE

2010-10-09 14:29:44

J2MEfontcolor

2009-06-23 11:30:16

RMSJ2ME

2010-09-29 12:45:50

J2ME

2011-12-02 10:37:14

JavaJ2ME

2010-09-29 10:15:35

JDKJ2EEJ2SE

2009-06-11 09:19:38

netbeans实例J2ME游戏

2010-09-29 15:17:22

J2MEDisplay类

2010-09-29 09:19:39

J2ME开发工具

2010-10-09 16:28:51

J2MEDisplay类

2010-09-29 09:59:22

J2ME配置

2010-09-30 13:11:59

J2MECanvas

2010-09-29 15:45:49

J2MEFontColor

2009-06-30 15:49:00

J2ME编程

2010-09-30 08:49:17

cookieJ2ME

2009-06-17 11:27:00

setClip方法J2ME
点赞
收藏

51CTO技术栈公众号