J2ME GUI实战之四 ----------LWUIT的Button使用以及窗体布局

function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();} 本文来自:http://blog.csdn.net/hellogv/ ,转载必须注明出处!
J2ME GUI实战之四 ----------LWUIT的Button使用以及窗体布局BorderLayout,就是把窗体布局分成东、南、西、北、中这5部分
J2ME GUI实战之四 ----------LWUIT的Button使用以及窗体布局//BoxLayout-X,就是把控件从左往右排列
J2ME GUI实战之四 ----------LWUIT的Button使用以及窗体布局//BoxLayout-Y,就是把控件从上往下排列
J2ME GUI实战之四 ----------LWUIT的Button使用以及窗体布局//FlowLayout,就是把控件按行排列,一行装不下则放到第二行......
J2ME GUI实战之四 ----------LWUIT的Button使用以及窗体布局//GridLayout,这就是实现九宫图的排列方式!!!!!

LWUIT要求使用Form和任何控件,都要设置窗体布局。OK,现在回顾一下以前所实现的九宫图,其原理是这样:九宫图本质就是九个按钮按照GridLayout排列,并且按钮附带图标,而且按钮获得焦点(按钮切换)时,会显示特效。
用过J2SE GUI的朋友,应该对这些不陌生,没接触过J2SE GUI的朋友也许需要一点时间消化一下。
OK,以下代码同样修改自Sample例子里面的,多余的话就不说了:
  1. /*
  2. *Copyright?2008SunMicrosystems,Inc.Allrightsreserved.
  3. *Useissubjecttolicenseterms.
  4. *
  5. */
  6. packagecom.sun.lwuit.uidemo;
  7. importcom.sun.lwuit.Button;
  8. importcom.sun.lwuit.Form;
  9. importcom.sun.lwuit.events.ActionEvent;
  10. importcom.sun.lwuit.events.ActionListener;
  11. importcom.sun.lwuit.layouts.BorderLayout;
  12. importcom.sun.lwuit.layouts.BoxLayout;
  13. importcom.sun.lwuit.layouts.FlowLayout;
  14. importcom.sun.lwuit.layouts.GridLayout;
  15. /**
  16. *本例演示如何布局窗体控件
  17. */
  18. publicclassLayoutDemoimplementsActionListener{
  19. publicFormform=newForm("LayoutDemo");
  20. privateButtonborder;
  21. privateButtonboxY;
  22. privateButtonboxX;
  23. privateButtonflow;
  24. privateButtongrid;
  25. LayoutDemo(){
  26. form.setLayout(newBoxLayout(BoxLayout.Y_AXIS));
  27. //BorderLayout,就是把窗体布局分成东、南、西、北、中这5部分
  28. border=newButton("BorderLayout");
  29. //顾名思义,设置按钮背景的透明度,范围0~255,可以用Util的资源编辑器来预先修改
  30. border.getStyle().setBgTransparency(100);
  31. //每个button都需要设计监听事件
  32. border.addActionListener(this);
  33. //BoxLayout-Y,就是把控件从上往下排列
  34. boxY=newButton("BoxLayout-Y");
  35. boxY.getStyle().setBgTransparency(100);
  36. boxY.addActionListener(this);
  37. //BoxLayout-X,就是把控件从左往右排列
  38. boxX=newButton("BoxLayout-X");
  39. boxX.getStyle().setBgTransparency(100);
  40. boxX.addActionListener(this);
  41. //FlowLayout,就是把控件按行排列,一行装不下则放到第二行......
  42. flow=newButton("FlowLayout");
  43. flow.getStyle().setBgTransparency(100);
  44. flow.addActionListener(this);
  45. //GridLayout,这就是实现九宫图的排列方式!!!!!
  46. grid=newButton("GridLayout");
  47. grid.getStyle().setBgTransparency(100);
  48. grid.addActionListener(this);
  49. addComponents(form);
  50. form.show();
  51. }
  52. privatevoidaddComponents(finalFormf){
  53. f.removeAll();
  54. f.addComponent(boxY);
  55. f.addComponent(boxX);
  56. f.addComponent(border);
  57. f.addComponent(flow);
  58. f.addComponent(grid);
  59. }
  60. publicvoidactionPerformed(ActionEventarg0){
  61. Stringbutton_name=((Button)(arg0.getSource())).getText();
  62. if(button_name.equals("BorderLayout"))
  63. {
  64. form.setLayout(newBorderLayout());
  65. form.removeAll();
  66. form.setScrollable(false);
  67. form.addComponent(BorderLayout.NORTH,border);
  68. form.addComponent(BorderLayout.EAST,boxY);
  69. form.addComponent(BorderLayout.CENTER,grid);
  70. form.addComponent(BorderLayout.WEST,flow);
  71. form.addComponent(BorderLayout.SOUTH,boxX);
  72. form.show();
  73. }
  74. elseif(button_name.equals("BoxLayout-Y"))
  75. {
  76. form.setLayout(newBoxLayout(BoxLayout.Y_AXIS));
  77. form.setScrollable(false);
  78. addComponents(form);
  79. form.show();
  80. }
  81. elseif(button_name.equals("FlowLayout"))
  82. {
  83. form.setLayout(newFlowLayout());
  84. form.setScrollable(false);
  85. addComponents(form);
  86. form.show();
  87. }
  88. elseif(button_name.equals("GridLayout"))
  89. {
  90. form.setLayout(newGridLayout(3,2));
  91. form.setScrollable(false);
  92. addComponents(form);
  93. form.show();
  94. }
  95. elseif(button_name.equals("BoxLayout-X"))
  96. {
  97. form.setLayout(newBoxLayout(BoxLayout.X_AXIS));
  98. form.setScrollable(true);
  99. addComponents(form);
  100. form.show();
  101. }
  102. }
  103. }

你可能感兴趣的:(.net,J2SE,F#,Blog,sun)