本文来自:http://blog.youkuaiyun.com/hellogv/ ,转载必须注明出处!
文章写到这里,想必大家也对LWUIT有个大概的了解了,至少也该知道LWUIT可以做些什么。九宫图只是LWUIT的Button控件的典型应用而已,在LWUIT里面,还有很多在J2SE GUI可以见到的控件,例如文本需要介绍的Label、CheckBox、RadioButton等。
Label一般用于显示而已,CheckBox作为复选框,可以让你多选,RadioButton作为单选框,仅仅让你多选一。
OK,废话少水,直奔代码:
文章写到这里,想必大家也对LWUIT有个大概的了解了,至少也该知道LWUIT可以做些什么。九宫图只是LWUIT的Button控件的典型应用而已,在LWUIT里面,还有很多在J2SE GUI可以见到的控件,例如文本需要介绍的Label、CheckBox、RadioButton等。
Label一般用于显示而已,CheckBox作为复选框,可以让你多选,RadioButton作为单选框,仅仅让你多选一。
OK,废话少水,直奔代码:
- /*
- *Copyright?2008SunMicrosystems,Inc.Allrightsreserved.
- *Useissubjecttolicenseterms.
- *
- */
- packagecom.sun.lwuit.uidemo;
- importcom.sun.lwuit.ButtonGroup;
- importcom.sun.lwuit.CheckBox;
- importcom.sun.lwuit.Command;
- importcom.sun.lwuit.Component;
- importcom.sun.lwuit.Dialog;
- importcom.sun.lwuit.Form;
- importcom.sun.lwuit.Label;
- importcom.sun.lwuit.RadioButton;
- importcom.sun.lwuit.layouts.BoxLayout;
- importcom.sun.lwuit.events.ActionEvent;
- importcom.sun.lwuit.events.ActionListener;
- /**
- *演示RadioButton、CheckBox、Label的使用
- */
- publicclassRb_Cb_LbimplementsActionListener{
- publicFormform=newForm("Rb_Cb_Lb");
- publicCommandbackCommand=newCommand("Back",1);//返回按钮
- publicCommandselectCommand=newCommand("select",2);//确认选择的按钮
- //ButtonGroup就是把所有的RadioButton放在一起,选择唯一
- publicButtonGroupgroup=newButtonGroup();
- Rb_Cb_Lb(){
- form.setLayout(newBoxLayout(BoxLayout.Y_AXIS));
- form.addCommand(backCommand);
- form.addCommand(selectCommand);
- form.setCommandListener(this);
- Labelcdlabel=newLabel("CheckBox:");
- cdlabel.getStyle().setMargin(Component.BOTTOM,0);
- form.addComponent(cdlabel);
- finalCheckBoxfirstCB=newCheckBox("FirstCheckBox");
- firstCB.getStyle().setMargin(Component.TOP,1);
- form.addComponent(firstCB);
- finalCheckBoxsecondCB=newCheckBox("SecondCheckBox");
- secondCB.getStyle().setMargin(0,5,2,2);
- form.addComponent(secondCB);
- Labelrblabel=newLabel("RadioButton:");
- rblabel.getStyle().setMargin(Component.BOTTOM,0);
- form.addComponent(rblabel);
- finalRadioButtonfirstRB=newRadioButton("FirstRadioButton");
- form.addComponent(firstRB);
- finalRadioButtonsecondRB=newRadioButton("SecondRadioButton");
- form.addComponent(secondRB);
- finalRadioButtonthirdRB=newRadioButton("ThirdRadioButton");
- form.addComponent(thirdRB);
- ActionListenerlistener=newActionListener(){
- //这里是处理CheckBox、RadioButton点击事件处理程序
- publicvoidactionPerformed(ActionEventarg0){
- Objectsource=arg0.getSource();
- if(source==firstCB)
- Dialog.show("Rb_Cb_Lb","firstCB","OK",null);
- elseif(source==secondCB)
- Dialog.show("Rb_Cb_Lb","secondCB","OK",null);
- elseif(source==firstRB)
- Dialog.show("Rb_Cb_Lb","firstRB","OK",null);
- elseif(source==secondRB)
- Dialog.show("Rb_Cb_Lb","secondRB","OK",null);
- elseif(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);
- };
- publicvoidactionPerformed(ActionEventarg0){
- //这里处理Command以及判断ButtonGroup所选中的RadioButton
- Commandcmd=arg0.getCommand();
- if(cmd==backCommand)
- UIDemoMIDlet.backToMainMenu();
- elseif(cmd==selectCommand)
- {
- Stringstr="ButtonCount:"+group.getButtonCount()+'\n'+
- "SelectedIndex:"+group.getSelectedIndex()+'\n'+
- "RadioButton:"+(group.getRadioButton(group.getSelectedIndex()).getText());
- Dialog.show("Rb_Cb_Lb",str,"OK",null);
- }
- }
- }