开发学院软件开发Java J2ME GUI实战之六 ----------LWUIT的La... 阅读

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

 2009-09-12 00:00:00 来源:WEB开发网   
核心提示:文章写到这里,想必大家也对LWUIT有个大概的了解了,J2ME GUI实战之六 ----------LWUIT的Label、CheckBox、RadioButton,至少也该知道LWUIT可以做些什么,九宫图只是LWUIT的Button控件的典型应用而已,OK,废话少水,在LWUIT里面,还有很多在J2SE GUI可以

文章写到这里,想必大家也对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.
   8. import com.sun.lwuit.ButtonGroup;
   9. import com.sun.lwuit.CheckBox;
  10. import com.sun.lwuit.Command;
  11. import com.sun.lwuit.Component;
  12. import com.sun.lwuit.Dialog;
  13. import com.sun.lwuit.Form;
  14. import com.sun.lwuit.Label;
  15. import com.sun.lwuit.RadioButton;
  16. import com.sun.lwuit.layouts.BoxLayout;
  17. import com.sun.lwuit.events.ActionEvent;
  18. import com.sun.lwuit.events.ActionListener;
  19.
  20. /**
  21.  * 演示RadioButton、CheckBox、Label的使用
  22.  */
  23. public class Rb_Cb_Lb implements ActionListener {
  24. public Form form = new Form("Rb_Cb_Lb");
  25. public  Command backCommand = new Command("Back", 1);//返回按钮
  26. public  Command selectCommand = new Command("select", 2);//确认选择的按钮
  27. //ButtonGroup就是把所有的RadioButton放在一起,选择唯一
  28. public ButtonGroup group = new ButtonGroup();
  29.     Rb_Cb_Lb(){
  30.         form.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
  31.         form.addCommand(backCommand);
  32.         form.addCommand(selectCommand);
  33.         form.setCommandListener(this);
  34.
  35.         Label cdlabel = new Label("CheckBox:");
  36.         cdlabel.getStyle().setMargin(Component.BOTTOM, 0);
  37.         form.addComponent(cdlabel);
  38.
  39.         final CheckBox firstCB = new CheckBox("First CheckBox");
  40.         firstCB.getStyle().setMargin(Component.TOP, 1);
  41.         form.addComponent(firstCB);
  42.
  43.         final CheckBox secondCB = new CheckBox("Second CheckBox");
  44.         secondCB.getStyle().setMargin(0, 5, 2, 2);
  45.         form.addComponent(secondCB);
  46.
  47.         Label rblabel = new Label("RadioButton:");
  48.         rblabel.getStyle().setMargin(Component.BOTTOM, 0);
  49.         form.addComponent(rblabel);
  50.
  51.         final RadioButton firstRB = new RadioButton("First RadioButton");
  52.         form.addComponent(firstRB);
  53.        
  54.         final RadioButton secondRB = new RadioButton("Second RadioButton");
  55.         form.addComponent(secondRB);
  56.
  57.         final RadioButton thirdRB = new RadioButton("Third RadioButton");
  58.         form.addComponent(thirdRB);
  59.
  60.         ActionListener listener = new ActionListener() {
  61.         //这里是处理CheckBox、RadioButton 点击事件处理程序
  62.             public void actionPerformed(ActionEvent arg0) {
  63.                 Object source = arg0.getSource();
  64.                 if(source==firstCB)
  65.                     Dialog.show("Rb_Cb_Lb","firstCB", "OK", null);
  66.                 else if(source==secondCB)
  67.                     Dialog.show("Rb_Cb_Lb","secondCB", "OK", null);
  68.                 else if(source==firstRB)
  69.                     Dialog.show("Rb_Cb_Lb","firstRB", "OK", null);
  70.                 else if(source==secondRB)
  71.                     Dialog.show("Rb_Cb_Lb","secondRB", "OK", null);
  72.                 else if(source==thirdRB)
  73.                     Dialog.show("Rb_Cb_Lb","thirdRB", "OK", null);
  74.             }
  75.         };
  76.
  77.         //往ButtonGroup加入radiobutton
  78.         group.add(firstRB);
  79.         group.add(secondRB);
  80.         group.add(thirdRB);
  81.         firstRB.addActionListener(listener);//加入事件监听
  82.         secondRB.addActionListener(listener);//加入事件监听
  83.         thirdRB.addActionListener(listener);//加入事件监听
  84.
  85.         //----为复选框加入事件监听
  86.         firstCB.addActionListener(listener);
  87.         secondCB.addActionListener(listener);
  88.     };
  89.
  90.     public void actionPerformed(ActionEvent arg0) {
  91.     //这里处理Command 以及 判断ButtonGroup所选中的RadioButton
  92.         Command cmd=arg0.getCommand();
  93.         if(cmd==backCommand)
  94.             UIDemoMIDlet.backToMainMenu();
  95.         else if(cmd==selectCommand)
  96.         {
  97.             String str="ButtonCount:"+group.getButtonCount()+'\n'+
  98.                        "SelectedIndex:"+group.getSelectedIndex()+'\n'+
  99.                        "RadioButton:"+(group.getRadioButton(group.getSelectedIndex()).getText());
 100.             Dialog.show("Rb_Cb_Lb",str, "OK", null);
 101.         }
 102.     }
 103. }

Tags:JME GUI 实战

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接