现在的位置: 首页 > 综合 > 正文

J2ME GUI实战之六 ———-LWUIT的Label、CheckBox、RadioButton

2013年10月12日 ⁄ 综合 ⁄ 共 3909字 ⁄ 字号 评论关闭
 本文来自:http://blog.csdn.net/hellogv/ ,转载必须注明出处!

    文章写到这里,想必大家也对LWUIT有个大概的了解了,至少也该知道LWUIT可以做些什么。九宫图只是LWUIT的Button控件的典型应用而已,在LWUIT里面,还有很多在J2SE GUI可以见到的控件,例如文本需要介绍的Label、CheckBox、RadioButton等。
    Label一般用于显示而已,CheckBox作为复选框,可以让你多选,RadioButton作为单选框,仅仅让你多选一。
    OK,废话少水,直奔代码:
  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.ButtonGroup;
  8. import com.sun.lwuit.CheckBox;
  9. import com.sun.lwuit.Command;
  10. import com.sun.lwuit.Component;
  11. import com.sun.lwuit.Dialog;
  12. import com.sun.lwuit.Form;
  13. import com.sun.lwuit.Label;
  14. import com.sun.lwuit.RadioButton;
  15. import com.sun.lwuit.layouts.BoxLayout;
  16. import com.sun.lwuit.events.ActionEvent;
  17. import com.sun.lwuit.events.ActionListener;
  18. /**
  19.  * 演示RadioButton、CheckBox、Label的使用
  20.  */
  21. public class Rb_Cb_Lb implements ActionListener {
  22. public Form form = new Form("Rb_Cb_Lb");
  23. public  Command backCommand = new Command("Back"1);//返回按钮
  24. public  Command selectCommand = new Command("select"2);//确认选择的按钮
  25. //ButtonGroup就是把所有的RadioButton放在一起,选择唯一
  26. public ButtonGroup group = new ButtonGroup();
  27.     Rb_Cb_Lb(){
  28.         form.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
  29.         form.addCommand(backCommand);
  30.         form.addCommand(selectCommand);
  31.         form.setCommandListener(this);
  32.         Label cdlabel = new Label("CheckBox:");
  33.         cdlabel.getStyle().setMargin(Component.BOTTOM, 0);
  34.         form.addComponent(cdlabel);
  35.         final CheckBox firstCB = new CheckBox("First CheckBox");
  36.         firstCB.getStyle().setMargin(Component.TOP, 1);
  37.         form.addComponent(firstCB);
  38.         final CheckBox secondCB = new CheckBox("Second CheckBox");
  39.         secondCB.getStyle().setMargin(0522);
  40.         form.addComponent(secondCB);
  41.         Label rblabel = new Label("RadioButton:");
  42.         rblabel.getStyle().setMargin(Component.BOTTOM, 0);
  43.         form.addComponent(rblabel);
  44.         final RadioButton firstRB = new RadioButton("First RadioButton");
  45.         form.addComponent(firstRB);
  46.         
  47.         final RadioButton secondRB = new RadioButton("Second RadioButton");
  48.         form.addComponent(secondRB);
  49.         final RadioButton thirdRB = new RadioButton("Third RadioButton");
  50.         form.addComponent(thirdRB);
  51.         ActionListener listener = new ActionListener() {
  52.         //这里是处理CheckBox、RadioButton 点击事件处理程序
  53.             public void actionPerformed(ActionEvent arg0) {
  54.                 Object source = arg0.getSource();
  55.                 if(source==firstCB)
  56.                     Dialog.show("Rb_Cb_Lb","firstCB""OK"null);
  57.                 else if(source==secondCB)
  58.                     Dialog.show("Rb_Cb_Lb","secondCB""OK"null);
  59.                 else if(source==firstRB)
  60.                     Dialog.show("Rb_Cb_Lb","firstRB""OK"null);
  61.                 else if(source==secondRB)
  62.                     Dialog.show("Rb_Cb_Lb","secondRB""OK"null);
  63.                 else if(source==thirdRB)
  64.                     Dialog.show("Rb_Cb_Lb","thirdRB""OK"null);
  65.             }
  66.         };
  67.         //往ButtonGroup加入radiobutton
  68.         group.add(firstRB);
  69.         group.add(secondRB);
  70.         group.add(thirdRB);
  71.         firstRB.addActionListener(listener);//加入事件监听
  72.         secondRB.addActionListener(listener);//加入事件监听
  73.         thirdRB.addActionListener(listener);//加入事件监听
  74.         //----为复选框加入事件监听
  75.         firstCB.addActionListener(listener);
  76.         secondCB.addActionListener(listener);
  77.     };
  78.     public void actionPerformed(ActionEvent arg0) {
  79.     //这里处理Command 以及 判断ButtonGroup所选中的RadioButton
  80.         Command cmd=arg0.getCommand();
  81.         if(cmd==backCommand)
  82.             UIDemoMIDlet.backToMainMenu();
  83.         else if(cmd==selectCommand)
  84.         {
  85.             String str="ButtonCount:"+group.getButtonCount()+'/n'+
  86.                        "SelectedIndex:"+group.getSelectedIndex()+'/n'+
  87.                        "RadioButton:"+(group.getRadioButton(group.getSelectedIndex()).getText());
  88.             Dialog.show("Rb_Cb_Lb",str, "OK"null);
  89.         }
  90.     }
  91. }

抱歉!评论已关闭.