J2ME GUI实战之六 ----------LWUIT的Label、CheckBox、RadioButton
2008-09-15 16:59
375 查看
本文来自:http://blog.csdn.net/hellogv/ ,转载必须注明出处!
文章写到这里,想必大家也对LWUIT有个大概的了解了,至少也该知道LWUIT可以做些什么。九宫图只是LWUIT的Button控件的典型应用而已,在LWUIT里面,还有很多在J2SE GUI可以见到的控件,例如文本需要介绍的Label、CheckBox、RadioButton等。
Label一般用于显示而已,CheckBox作为复选框,可以让你多选,RadioButton作为单选框,仅仅让你多选一。
OK,废话少水,直奔代码:
/*
* Copyright ?2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
*/
package com.sun.lwuit.uidemo;
import com.sun.lwuit.ButtonGroup;
import com.sun.lwuit.CheckBox;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.RadioButton;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
/**
* 演示RadioButton、CheckBox、Label的使用
*/
public class Rb_Cb_Lb implements ActionListener {
public Form form = new Form("Rb_Cb_Lb");
public Command backCommand = new Command("Back", 1);//返回按钮
public Command selectCommand = new Command("select", 2);//确认选择的按钮
//ButtonGroup就是把所有的RadioButton放在一起,选择唯一
public ButtonGroup group = new ButtonGroup();
Rb_Cb_Lb(){
form.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
form.addCommand(backCommand);
form.addCommand(selectCommand);
form.setCommandListener(this);
Label cdlabel = new Label("CheckBox:");
cdlabel.getStyle().setMargin(Component.BOTTOM, 0);
form.addComponent(cdlabel);
final CheckBox firstCB = new CheckBox("First CheckBox");
firstCB.getStyle().setMargin(Component.TOP, 1);
form.addComponent(firstCB);
final CheckBox secondCB = new CheckBox("Second CheckBox");
secondCB.getStyle().setMargin(0, 5, 2, 2);
form.addComponent(secondCB);
Label rblabel = new Label("RadioButton:");
rblabel.getStyle().setMargin(Component.BOTTOM, 0);
form.addComponent(rblabel);
final RadioButton firstRB = new RadioButton("First RadioButton");
form.addComponent(firstRB);
final RadioButton secondRB = new RadioButton("Second RadioButton");
form.addComponent(secondRB);
final RadioButton thirdRB = new RadioButton("Third RadioButton");
form.addComponent(thirdRB);
ActionListener listener = new ActionListener() {
//这里是处理CheckBox、RadioButton 点击事件处理程序
public void actionPerformed(ActionEvent arg0) {
Object source = arg0.getSource();
if(source==firstCB)
Dialog.show("Rb_Cb_Lb","firstCB", "OK", null);
else if(source==secondCB)
Dialog.show("Rb_Cb_Lb","secondCB", "OK", null);
else if(source==firstRB)
Dialog.show("Rb_Cb_Lb","firstRB", "OK", null);
else if(source==secondRB)
Dialog.show("Rb_Cb_Lb","secondRB", "OK", null);
else if(source==thirdRB)
Dialog.show("Rb_Cb_Lb","thirdRB", "OK", null);
}
};
//往ButtonGroup加入radiobutton
group.add(firstRB);
group.add(secondRB);
group.add(thirdRB);
firstRB.addActionListener(listener);//加入事件监听
secondRB.addActionListener(listener);//加入事件监听
thirdRB.addActionListener(listener);//加入事件监听
//----为复选框加入事件监听
firstCB.addActionListener(listener);
secondCB.addActionListener(listener);
};
public void actionPerformed(ActionEvent arg0) {
//这里处理Command 以及 判断ButtonGroup所选中的RadioButton
Command cmd=arg0.getCommand();
if(cmd==backCommand)
UIDemoMIDlet.backToMainMenu();
else if(cmd==selectCommand)
{
String str="ButtonCount:"+group.getButtonCount()+'/n'+
"SelectedIndex:"+group.getSelectedIndex()+'/n'+
"RadioButton:"+(group.getRadioButton(group.getSelectedIndex()).getText());
Dialog.show("Rb_Cb_Lb",str, "OK", null);
}
}
}
文章写到这里,想必大家也对LWUIT有个大概的了解了,至少也该知道LWUIT可以做些什么。九宫图只是LWUIT的Button控件的典型应用而已,在LWUIT里面,还有很多在J2SE GUI可以见到的控件,例如文本需要介绍的Label、CheckBox、RadioButton等。
Label一般用于显示而已,CheckBox作为复选框,可以让你多选,RadioButton作为单选框,仅仅让你多选一。
OK,废话少水,直奔代码:
/*
* Copyright ?2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
*/
package com.sun.lwuit.uidemo;
import com.sun.lwuit.ButtonGroup;
import com.sun.lwuit.CheckBox;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.RadioButton;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
/**
* 演示RadioButton、CheckBox、Label的使用
*/
public class Rb_Cb_Lb implements ActionListener {
public Form form = new Form("Rb_Cb_Lb");
public Command backCommand = new Command("Back", 1);//返回按钮
public Command selectCommand = new Command("select", 2);//确认选择的按钮
//ButtonGroup就是把所有的RadioButton放在一起,选择唯一
public ButtonGroup group = new ButtonGroup();
Rb_Cb_Lb(){
form.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
form.addCommand(backCommand);
form.addCommand(selectCommand);
form.setCommandListener(this);
Label cdlabel = new Label("CheckBox:");
cdlabel.getStyle().setMargin(Component.BOTTOM, 0);
form.addComponent(cdlabel);
final CheckBox firstCB = new CheckBox("First CheckBox");
firstCB.getStyle().setMargin(Component.TOP, 1);
form.addComponent(firstCB);
final CheckBox secondCB = new CheckBox("Second CheckBox");
secondCB.getStyle().setMargin(0, 5, 2, 2);
form.addComponent(secondCB);
Label rblabel = new Label("RadioButton:");
rblabel.getStyle().setMargin(Component.BOTTOM, 0);
form.addComponent(rblabel);
final RadioButton firstRB = new RadioButton("First RadioButton");
form.addComponent(firstRB);
final RadioButton secondRB = new RadioButton("Second RadioButton");
form.addComponent(secondRB);
final RadioButton thirdRB = new RadioButton("Third RadioButton");
form.addComponent(thirdRB);
ActionListener listener = new ActionListener() {
//这里是处理CheckBox、RadioButton 点击事件处理程序
public void actionPerformed(ActionEvent arg0) {
Object source = arg0.getSource();
if(source==firstCB)
Dialog.show("Rb_Cb_Lb","firstCB", "OK", null);
else if(source==secondCB)
Dialog.show("Rb_Cb_Lb","secondCB", "OK", null);
else if(source==firstRB)
Dialog.show("Rb_Cb_Lb","firstRB", "OK", null);
else if(source==secondRB)
Dialog.show("Rb_Cb_Lb","secondRB", "OK", null);
else if(source==thirdRB)
Dialog.show("Rb_Cb_Lb","thirdRB", "OK", null);
}
};
//往ButtonGroup加入radiobutton
group.add(firstRB);
group.add(secondRB);
group.add(thirdRB);
firstRB.addActionListener(listener);//加入事件监听
secondRB.addActionListener(listener);//加入事件监听
thirdRB.addActionListener(listener);//加入事件监听
//----为复选框加入事件监听
firstCB.addActionListener(listener);
secondCB.addActionListener(listener);
};
public void actionPerformed(ActionEvent arg0) {
//这里处理Command 以及 判断ButtonGroup所选中的RadioButton
Command cmd=arg0.getCommand();
if(cmd==backCommand)
UIDemoMIDlet.backToMainMenu();
else if(cmd==selectCommand)
{
String str="ButtonCount:"+group.getButtonCount()+'/n'+
"SelectedIndex:"+group.getSelectedIndex()+'/n'+
"RadioButton:"+(group.getRadioButton(group.getSelectedIndex()).getText());
Dialog.show("Rb_Cb_Lb",str, "OK", null);
}
}
}
相关文章推荐
- J2ME GUI实战之六 ----------LWUIT的Label、CheckBox、RadioButton
- J2ME GUI实战之四 ----------LWUIT的Button使用以及窗体布局
- J2ME GUI实战之四 ----------LWUIT的Button使用以及窗体布局
- J2ME GUI实战之七 ----------LWUIT的Tabbed分页、Text文本
- J2ME GUI实战之五 ----------LWUIT的绘图功能
- J2ME GUI实战之七 ----------LWUIT的Tabbed分页、Text文本
- J2ME GUI实战之八 ----------LWUIT的List控件
- J2ME GUI实战之五 ----------LWUIT的绘图功能
- J2ME GUI实战之八 ----------LWUIT的List控件
- MVC-Html.Label(TextBox、TextArea、RadioButton、CheckBox)
- J2ME GUI实战之一 ----------LWUIT简介
- J2ME GUI实战之一 ----------LWUIT简介
- J2ME GUI实战之三 ----------LWUIT实现切换特效
- J2ME GUI实战之9 ----------自定义控件布局,DIY Layout
- J2ME GUI实战之三 ----------LWUIT实现切换特效
- J2ME GUI实战之9 ----------自定义控件布局,DIY Layout
- J2ME GUI实战之二 ----------LWUIT实现手机九宫图
- J2ME GUI实战之二 ----------LWUIT实现手机九宫图
- Chapter 2. WinForm(控件TextBox、Label、checkbox、radiobutton,容器groupbox)
- 用python来做一个APP | python GUI 基础(实战)