J2ME GUI实战之二 ----------LWUIT实现手机九宫图

本文来自:http://blog.csdn.net/hellogv/ ,转载必须注明出处!
本文的源代码,已放到 http://download.csdn.net/user/hellogv 供大家下载!

首先,想让大家看看九宫图实现之后的界面:
J2ME GUI实战之二 ----------LWUIT实现手机九宫图

本文所用的代码,是来自LWUIT所提供的Sample例子,但是对例子做出以下修改:
1.设计自主的res资源文件
2.把builde.xml里面的<target name="post-compile"> 。。。。。</target> 删除
3.把所用的res文件放到一个名为res的文件夹,并与源代码放在一起
4.把ResourceBuilder.jar也删除,util文件夹里的ResourceBuilder.jar绝对不能删,是用于设计res文件

针对以上4点,说明一下为什么这么做:
原Sample例子的res文件,是使用util文件夹里的ResourceEdit.exe设计,而且还在builde.xml中使用ResourceBuilder.jar再重新生成一次res文件并与源文件组合,这显然有点冗余,因为直接用ResourceEdit.exe设计的res文件放到源文件目录里即可。也许Sample作者直接使用ANT编译或者是其他IDE要求,本文所用的IDE为netbeans。

OK,接下来就是源代码修改了,Sample源代码可是写得很长很长,要看也得花不少时间,但是,我保证大家看完后一定领略到JAVA大牛的编程功力之深!!!为了方便以后使用,所以我把Sample中实现九宫图和一些小操作的代码保留,其他都去掉了,请JAVA大牛切勿见笑哈。
以下给出关键代码:

  1. /*
  2. *Copyright?2008SunMicrosystems,Inc.Allrightsreserved.
  3. *Useissubjecttolicenseterms.
  4. *
  5. */
  6. packagecom.sun.lwuit.uidemo;
  7. importcom.sun.lwuit.Button;
  8. importcom.sun.lwuit.animations.CommonTransitions;
  9. importcom.sun.lwuit.Command;
  10. importcom.sun.lwuit.Component;
  11. importcom.sun.lwuit.Dialog;
  12. importcom.sun.lwuit.Display;
  13. importcom.sun.lwuit.Form;
  14. importcom.sun.lwuit.Image;
  15. importcom.sun.lwuit.Label;
  16. importcom.sun.lwuit.animations.Transition;
  17. importcom.sun.lwuit.animations.Transition3D;
  18. importcom.sun.lwuit.events.ActionEvent;
  19. importcom.sun.lwuit.events.ActionListener;
  20. importcom.sun.lwuit.events.FocusListener;
  21. importcom.sun.lwuit.layouts.GridLayout;
  22. importcom.sun.lwuit.plaf.Style;
  23. importcom.sun.lwuit.plaf.UIManager;
  24. importcom.sun.lwuit.util.Resources;
  25. publicclassUIDemoMIDletextendsjavax.microedition.midlet.MIDletimplementsActionListener{
  26. privatestaticfinalCommandexitCommand=newCommand("Exit",1);
  27. privateString[]btn_name=newString[]{//九宫图的名称,同时也是图标的名称
  28. "Image1","Image2","Image3","Image4","Image5","Image6","Image7",
  29. "Image8","Image9"
  30. };
  31. privatestaticTransitioncomponentTransitions;
  32. privatestaticFormmainMenu;//主窗体
  33. privateintcols;
  34. privateintelementWidth;
  35. protectedvoidstartApp(){
  36. try{
  37. Display.init(this);
  38. Resourcesr=Resources.open("/res/resources.res");//读取资源文件
  39. setMainForm(r);
  40. }catch(Throwableex){
  41. ex.printStackTrace();
  42. Dialog.show("Exception",ex.getMessage(),"OK",null);//hellogv最爱的提示框弹出方式
  43. }
  44. }
  45. protectedvoidpauseApp(){
  46. }
  47. protectedvoiddestroyApp(booleanarg0){
  48. }
  49. publicstaticvoidsetMenuTransition(Transitionin,Transitionout){
  50. mainMenu.setMenuTransitions(in,out);
  51. UIManager.getInstance().getLookAndFeel().setDefaultMenuTransitionIn(in);
  52. UIManager.getInstance().getLookAndFeel().setDefaultMenuTransitionOut(out);
  53. }
  54. publicstaticvoidsetComponentTransition(Transitiont){
  55. componentTransitions=t;
  56. mainMenu.setSmoothScrolling(false);
  57. }
  58. publicstaticTransitiongetComponentTransition(){
  59. returncomponentTransitions;
  60. }
  61. publicstaticvoidbackToMainMenu(){
  62. mainMenu.refreshTheme();
  63. mainMenu.show();
  64. //forseries40devices
  65. System.gc();
  66. System.gc();
  67. }
  68. privatevoidsetMainForm(Resourcesr){
  69. //设置主题
  70. UIManager.getInstance().setThemeProps(r.getTheme("businessTheme"));
  71. mainMenu=newMainScreenForm("LWUITDemo");
  72. intwidth=Display.getInstance().getDisplayWidth();//取得全屏的宽度
  73. elementWidth=0;
  74. //效果,可以实现类似IPhone、S1智能机的TouchFlo的动态窗体切换效果!**********************
  75. mainMenu.setTransitionOutAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL,false,500));
  76. mainMenu.setTransitionInAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL,true,500));
  77. UIDemoMIDlet.setComponentTransition(Transition3D.createCube(600,false));
  78. //***************************************************************************************************************
  79. Image[]selectedImages=newImage[btn_name.length];//按钮被选中时的图片
  80. Image[]unselectedImages=newImage[btn_name.length];//按钮未被选中的图片
  81. for(inti=0;i<btn_name.length;i++){
  82. //---------读取选中时的图片
  83. selectedImages[i]=r.getImage(btn_name[i]);
  84. //--------读取未被选中的图片
  85. unselectedImages[i]=r.getImage(btn_name[i]);
  86. finalButtonbutton=newButton(btn_name[i],unselectedImages[i])
  87. {
  88. publicImagegetPressedIcon(){
  89. Imagei=getIcon();
  90. returni.scaled((int)(i.getWidth()*0.8),(int)(i.getHeight()*0.8));
  91. }
  92. };
  93. button.setRolloverIcon(selectedImages[i]);
  94. Styles=button.getStyle();
  95. s.setBorder(null);
  96. s.setBgTransparency(0);
  97. s.setBgSelectionColor(0xffffff);
  98. button.setAlignment(Label.CENTER);
  99. button.setTextPosition(Label.BOTTOM);
  100. mainMenu.addComponent(button);
  101. button.addActionListener(this);
  102. //按钮焦点处理,用于实现按钮特效
  103. button.addFocusListener(newFocusListener(){
  104. publicvoidfocusGained(Componentcmp){//按钮取得焦点时的特效
  105. if(componentTransitions!=null){
  106. mainMenu.replace(button,button,componentTransitions);
  107. }
  108. }
  109. publicvoidfocusLost(Componentcmp){}//按钮失去焦点时的特效
  110. });
  111. elementWidth=Math.max(button.getPreferredW(),elementWidth);
  112. }
  113. cols=width/elementWidth;
  114. introws=btn_name.length/cols;
  115. mainMenu.setLayout(newGridLayout(rows,cols));//GridLayout为九宫图的排列方式
  116. mainMenu.addCommand(exitCommand);
  117. mainMenu.setCommandListener(this);
  118. mainMenu.show();
  119. }
  120. /**
  121. *控件处理函数
  122. */
  123. publicvoidactionPerformed(ActionEventevt){
  124. try{//通过异常处理来判断事件来源
  125. Commandcmd=evt.getCommand();
  126. if(cmd.getCommandName().equals("Exit"))
  127. notifyDestroyed();
  128. }catch(Exceptione)
  129. {
  130. Stringsel_button=((Button)(evt.getSource())).getText();//取得所选按钮的名称
  131. if(sel_button.equals("Image1"))
  132. newAnimationDemo().form.show();
  133. elseif(sel_button.equals("Image2"))
  134. newPaintingDemo().form.show();
  135. elseif(sel_button.equals("Image3"))
  136. newLayoutDemo().form.show();
  137. }
  138. }
  139. }

好了,本文就介绍到这里,关于J2ME LWUIT的摸索还在继续,希望大家多多支持LWUIT,让它越做越好!

你可能感兴趣的:(编程,ant,ide,sun,Netbeans)