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

  <script>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();}</script> 本文来自:http://blog.csdn.net/hellogv/ ,转载必须注明出处!
BorderLayout,就是把窗体布局分成东、南、西、北、中这5部分
//BoxLayout-X,就是把控件从左往右排列
//BoxLayout-Y,就是把控件从上往下排列
//FlowLayout,就是把控件按行排列,一行装不下则放到第二行......
//GridLayout,这就是实现九宫图的排列方式!!!!!

    LWUIT要求使用Form和任何控件,都要设置窗体布局。OK,现在回顾一下以前所实现的九宫图,其原理是这样:九宫图本质就是九个按钮按照
GridLayout排列,并且按钮附带图标,而且按钮获得焦点(按钮切换)时,会显示特效。
    用过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. import com.sun.lwuit.Button;
  8. import com.sun.lwuit.Form;
  9. import com.sun.lwuit.events.ActionEvent;
  10. import com.sun.lwuit.events.ActionListener;
  11. import com.sun.lwuit.layouts.BorderLayout;
  12. import com.sun.lwuit.layouts.BoxLayout;
  13. import com.sun.lwuit.layouts.FlowLayout;
  14. import com.sun.lwuit.layouts.GridLayout;
  15. /**
  16.  *本例演示如何布局窗体控件
  17.  */
  18. public class LayoutDemo implements ActionListener {
  19.     public Form form = new Form("LayoutDemo");
  20.     private Button border;
  21.     private Button boxY;
  22.     private Button boxX;
  23.     private Button flow;
  24.     private Button grid;
  25.     LayoutDemo(){
  26.         
  27.         form.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
  28.         //BorderLayout,就是把窗体布局分成东、南、西、北、中这5部分
  29.         border = new Button("BorderLayout");
  30.         //顾名思义,设置按钮背景的透明度,范围0~255,可以用Util的资源编辑器来预先修改
  31.         border.getStyle().setBgTransparency(100);
  32.         //每个button都需要设计监听事件
  33.         border.addActionListener(this);
  34.         //BoxLayout-Y,就是把控件从上往下排列
  35.         boxY = new Button("BoxLayout-Y");
  36.         boxY.getStyle().setBgTransparency(100);
  37.         boxY.addActionListener(this);
  38.         //BoxLayout-X,就是把控件从左往右排列
  39.         boxX = new Button("BoxLayout-X");
  40.         boxX.getStyle().setBgTransparency(100);
  41.         boxX.addActionListener(this);
  42.         //FlowLayout,就是把控件按行排列,一行装不下则放到第二行......
  43.         flow = new Button("FlowLayout");
  44.         flow.getStyle().setBgTransparency(100);
  45.         flow.addActionListener(this);
  46.         //GridLayout,这就是实现九宫图的排列方式!!!!!
  47.         grid = new Button("GridLayout");
  48.         grid.getStyle().setBgTransparency(100);
  49.         grid.addActionListener(this);
  50.         
  51.         addComponents(form);
  52.         form.show();
  53.     }
  54.     private void addComponents(final Form f){
  55.         f.removeAll();
  56.         f.addComponent(boxY);
  57.         f.addComponent(boxX);
  58.         f.addComponent(border);
  59.         f.addComponent(flow);
  60.         f.addComponent(grid);
  61.     }
  62.     public void actionPerformed(ActionEvent arg0) {
  63.         String button_name=((Button)(arg0.getSource())).getText();
  64.         if(button_name.equals("BorderLayout"))
  65.         {
  66.             form.setLayout(new BorderLayout());
  67.             form.removeAll();
  68.             form.setScrollable(false);
  69.             form.addComponent(BorderLayout.NORTH, border);
  70.             form.addComponent(BorderLayout.EAST, boxY);
  71.             form.addComponent(BorderLayout.CENTER, grid);
  72.             form.addComponent(BorderLayout.WEST, flow);
  73.             form.addComponent(BorderLayout.SOUTH, boxX);
  74.             form.show();
  75.         }
  76.         else if(button_name.equals("BoxLayout-Y"))
  77.         {
  78.             form.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
  79.             form.setScrollable(false);
  80.             addComponents(form);
  81.             form.show();
  82.         }
  83.         else if(button_name.equals("FlowLayout"))
  84.         {
  85.             form.setLayout(new FlowLayout());
  86.             form.setScrollable(false);
  87.             addComponents(form);
  88.             form.show();
  89.         }
  90.         else if(button_name.equals("GridLayout"))
  91.         {
  92.             form.setLayout(new GridLayout(32));
  93.             form.setScrollable(false);
  94.             addComponents(form);
  95.             form.show();
  96.         }
  97.         else if(button_name.equals("BoxLayout-X"))
  98.         {
  99.             form.setLayout(new BoxLayout(BoxLayout.X_AXIS));
  100.             form.setScrollable(true);
  101.             addComponents(form);
  102.             form.show();
  103.         }
  104.     }
  105. }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值