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

BorderLayout,就是把窗体布局分成东、南、西、北、中这5部分

//BoxLayout-X,就是把控件从左往右排列

//BoxLayout-Y,就是把控件从上往下排列

//FlowLayout,就是把控件按行排列,一行装不下则放到第二行......

//GridLayout,这就是实现九宫图的排列方式!!!!!
LWUIT要求使用Form和任何控件,都要设置窗体布局。OK,现在回顾一下以前所实现的九宫图,其原理是这样:九宫图本质就是九个按钮按照GridLayout排列,并且按钮附带图标,而且按钮获得焦点(按钮切换)时,会显示特效。
用过J2SE GUI的朋友,应该对这些不陌生,没接触过J2SE GUI的朋友也许需要一点时间消化一下。
OK,以下代码同样修改自Sample例子里面的,多余的话就不说了:
/*
* Copyright ?2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
*/
package com.sun.lwuit.uidemo;
import com.sun.lwuit.Button;
import com.sun.lwuit.Form;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.layouts.FlowLayout;
import com.sun.lwuit.layouts.GridLayout;
/**
*本例演示如何布局窗体控件
*/
public class LayoutDemo implements ActionListener {
public Form form = new Form("LayoutDemo");
private Button border;
private Button boxY;
private Button boxX;
private Button flow;
private Button grid;
LayoutDemo(){
form.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
//BorderLayout,就是把窗体布局分成东、南、西、北、中这5部分
border = new Button("BorderLayout");
//顾名思义,设置按钮背景的透明度,范围0~255,可以用Util的资源编辑器来预先修改
border.getStyle().setBgTransparency(100);
//每个button都需要设计监听事件
border.addActionListener(this);
//BoxLayout-Y,就是把控件从上往下排列
boxY = new Button("BoxLayout-Y");
boxY.getStyle().setBgTransparency(100);
boxY.addActionListener(this);
//BoxLayout-X,就是把控件从左往右排列
boxX = new Button("BoxLayout-X");
boxX.getStyle().setBgTransparency(100);
boxX.addActionListener(this);
//FlowLayout,就是把控件按行排列,一行装不下则放到第二行......
flow = new Button("FlowLayout");
flow.getStyle().setBgTransparency(100);
flow.addActionListener(this);
//GridLayout,这就是实现九宫图的排列方式!!!!!
grid = new Button("GridLayout");
grid.getStyle().setBgTransparency(100);
grid.addActionListener(this);
addComponents(form);
form.show();
}
private void addComponents(final Form f){
f.removeAll();
f.addComponent(boxY);
f.addComponent(boxX);
f.addComponent(border);
f.addComponent(flow);
f.addComponent(grid);
}
public void actionPerformed(ActionEvent arg0) {
String button_name=((Button)(arg0.getSource())).getText();
if(button_name.equals("BorderLayout"))
{
form.setLayout(new BorderLayout());
form.removeAll();
form.setScrollable(false);
form.addComponent(BorderLayout.NORTH, border);
form.addComponent(BorderLayout.EAST, boxY);
form.addComponent(BorderLayout.CENTER, grid);
form.addComponent(BorderLayout.WEST, flow);
form.addComponent(BorderLayout.SOUTH, boxX);
form.show();
}
else if(button_name.equals("BoxLayout-Y"))
{
form.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
form.setScrollable(false);
addComponents(form);
form.show();
}
else if(button_name.equals("FlowLayout"))
{
form.setLayout(new FlowLayout());
form.setScrollable(false);
addComponents(form);
form.show();
}
else if(button_name.equals("GridLayout"))
{
form.setLayout(new GridLayout(3, 2));
form.setScrollable(false);
addComponents(form);
form.show();
}
else if(button_name.equals("BoxLayout-X"))
{
form.setLayout(new BoxLayout(BoxLayout.X_AXIS));
form.setScrollable(true);
addComponents(form);
form.show();
}
}
}
本文来自:http://blog.csdn.net/hellogv/ ,转载必须注明出处!
J2ME GUI实战之三 ----------LWUIT实现
J2ME GUI实战之五 ----------LWUIT的绘



