J2ME GUI实战之四 ----------LWUIT的Button使用以及窗体布局
2009-09-12 00:00:00 来源:WEB开发网用过J2SE GUI的朋友,应该对这些不陌生,没接触过J2SE GUI的朋友也许需要一点时间消化一下。
OK,以下代码同样修改自Sample例子里面的,多余的话就不说了:
1. /*
2. * Copyright ?2008 Sun Microsystems, Inc. All rights reserved.
3. * Use is subject to license terms.
4. *
5. */
6. package com.sun.lwuit.uidemo;
7.
8. import com.sun.lwuit.Button;
9. import com.sun.lwuit.Form;
10. import com.sun.lwuit.events.ActionEvent;
11. import com.sun.lwuit.events.ActionListener;
12. import com.sun.lwuit.layouts.BorderLayout;
13. import com.sun.lwuit.layouts.BoxLayout;
14. import com.sun.lwuit.layouts.FlowLayout;
15. import com.sun.lwuit.layouts.GridLayout;
16.
17. /**
18. *本例演示如何布局窗体控件
19. */
20. public class LayoutDemo implements ActionListener {
21.
22. public Form form = new Form("LayoutDemo");
23. private Button border;
24. private Button boxY;
25. private Button boxX;
26. private Button flow;
27. private Button grid;
28.
29. LayoutDemo(){
30.
31. form.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
32.
33. //BorderLayout,就是把窗体布局分成东、南、西、北、中这5部分
34. border = new Button("BorderLayout");
35. //顾名思义,设置按钮背景的透明度,范围0~255,可以用Util的资源编辑器来预先修改
36. border.getStyle().setBgTransparency(100);
37. //每个button都需要设计监听事件
38. border.addActionListener(this);
39.
40. //BoxLayout-Y,就是把控件从上往下排列
41. boxY = new Button("BoxLayout-Y");
42. boxY.getStyle().setBgTransparency(100);
43. boxY.addActionListener(this);
44.
45. //BoxLayout-X,就是把控件从左往右排列
46. boxX = new Button("BoxLayout-X");
47. boxX.getStyle().setBgTransparency(100);
48. boxX.addActionListener(this);
49.
50. //FlowLayout,就是把控件按行排列,一行装不下则放到第二行......
51. flow = new Button("FlowLayout");
52. flow.getStyle().setBgTransparency(100);
53. flow.addActionListener(this);
54.
55. //GridLayout,这就是实现九宫图的排列方式!!!!!
56. grid = new Button("GridLayout");
57. grid.getStyle().setBgTransparency(100);
58. grid.addActionListener(this);
59.
60. addComponents(form);
61. form.show();
62. }
63. private void addComponents(final Form f){
64. f.removeAll();
65. f.addComponent(boxY);
66. f.addComponent(boxX);
67. f.addComponent(border);
68. f.addComponent(flow);
69. f.addComponent(grid);
70. }
71.
72. public void actionPerformed(ActionEvent arg0) {
73. String button_name=((Button)(arg0.getSource())).getText();
74. if(button_name.equals("BorderLayout"))
75. {
76. form.setLayout(new BorderLayout());
77. form.removeAll();
78. form.setScrollable(false);
79. form.addComponent(BorderLayout.NORTH, border);
80. form.addComponent(BorderLayout.EAST, boxY);
81. form.addComponent(BorderLayout.CENTER, grid);
82. form.addComponent(BorderLayout.WEST, flow);
83. form.addComponent(BorderLayout.SOUTH, boxX);
84. form.show();
85. }
86. else if(button_name.equals("BoxLayout-Y"))
87. {
88. form.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
89. form.setScrollable(false);
90. addComponents(form);
91. form.show();
92. }
93. else if(button_name.equals("FlowLayout"))
94. {
95. form.setLayout(new FlowLayout());
96. form.setScrollable(false);
97. addComponents(form);
98. form.show();
99. }
100. else if(button_name.equals("GridLayout"))
101. {
102. form.setLayout(new GridLayout(3, 2));
103. form.setScrollable(false);
104. addComponents(form);
105. form.show();
106. }
107. else if(button_name.equals("BoxLayout-X"))
108. {
109. form.setLayout(new BoxLayout(BoxLayout.X_AXIS));
110. form.setScrollable(true);
111. addComponents(form);
112. form.show();
113. }
114. }
115. }
- ››实战案例分析:高质量软文对网站百度排名的影响
- ››实战经验浅谈网站搬家后的优化工作
- ››GUI库:使本机应用程序具备Windows窗体的简易性
- ››实战Active Directory站点部署与管理,Active Dir...
- ››实战操作主机角色转移,Active Directory系列之十...
- ››实战经验:巧用微博推广淘宝网店
- ››实战iPhone GPS定位系统
- ››实战Linux环境配置DBD:Oracle模块
- ››实战DeviceIoControl系列之一:通过API访问设备驱...
- ››实战DeviceIoControl系列之二:获取软盘/硬盘/光盘...
- ››实战DeviceIoControl系列之三:制作磁盘镜像文件
- ››实战DeviceIoControl系列之四:获取硬盘的详细信息...
更多精彩
赞助商链接