WEB开发网
开发学院软件开发Java J2ME GUI实战之四 ----------LWUIT的Bu... 阅读

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

 2009-09-12 00:00:00 来源:WEB开发网   
核心提示: 用过J2SE GUI的朋友,应该对这些不陌生,J2ME GUI实战之四 ----------LWUIT的Button使用以及窗体布局(2),没接触过J2SE GUI的朋友也许需要一点时间消化一下,OK,就是把控件从上往下排列 41. boxY = new Button("BoxLayo

用过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. }

上一页  1 2 

Tags:JME GUI 实战

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接