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

本文来自:http://blog.csdn.net/hellogv/ ,转载必须注明出处!

文章写到这里,想必大家也对LWUIT有个大概的了解了,至少也该知道LWUIT可以做些什么。九宫图只是LWUIT的Button控件的典型应用而已,在LWUIT里面,还有很多在J2SE GUI可以见到的控件,例如文本需要介绍的Label、CheckBox、RadioButton等。
Label一般用于显示而已,CheckBox作为复选框,可以让你多选,RadioButton作为单选框,仅仅让你多选一。
OK,废话少水,直奔代码:
  1. /*
  2. *Copyright?2008SunMicrosystems,Inc.Allrightsreserved.
  3. *Useissubjecttolicenseterms.
  4. *
  5. */
  6. packagecom.sun.lwuit.uidemo;
  7. importcom.sun.lwuit.ButtonGroup;
  8. importcom.sun.lwuit.CheckBox;
  9. importcom.sun.lwuit.Command;
  10. importcom.sun.lwuit.Component;
  11. importcom.sun.lwuit.Dialog;
  12. importcom.sun.lwuit.Form;
  13. importcom.sun.lwuit.Label;
  14. importcom.sun.lwuit.RadioButton;
  15. importcom.sun.lwuit.layouts.BoxLayout;
  16. importcom.sun.lwuit.events.ActionEvent;
  17. importcom.sun.lwuit.events.ActionListener;
  18. /**
  19. *演示RadioButton、CheckBox、Label的使用
  20. */
  21. publicclassRb_Cb_LbimplementsActionListener{
  22. publicFormform=newForm("Rb_Cb_Lb");
  23. publicCommandbackCommand=newCommand("Back",1);//返回按钮
  24. publicCommandselectCommand=newCommand("select",2);//确认选择的按钮
  25. //ButtonGroup就是把所有的RadioButton放在一起,选择唯一
  26. publicButtonGroupgroup=newButtonGroup();
  27. Rb_Cb_Lb(){
  28. form.setLayout(newBoxLayout(BoxLayout.Y_AXIS));
  29. form.addCommand(backCommand);
  30. form.addCommand(selectCommand);
  31. form.setCommandListener(this);
  32. Labelcdlabel=newLabel("CheckBox:");
  33. cdlabel.getStyle().setMargin(Component.BOTTOM,0);
  34. form.addComponent(cdlabel);
  35. finalCheckBoxfirstCB=newCheckBox("FirstCheckBox");
  36. firstCB.getStyle().setMargin(Component.TOP,1);
  37. form.addComponent(firstCB);
  38. finalCheckBoxsecondCB=newCheckBox("SecondCheckBox");
  39. secondCB.getStyle().setMargin(0,5,2,2);
  40. form.addComponent(secondCB);
  41. Labelrblabel=newLabel("RadioButton:");
  42. rblabel.getStyle().setMargin(Component.BOTTOM,0);
  43. form.addComponent(rblabel);
  44. finalRadioButtonfirstRB=newRadioButton("FirstRadioButton");
  45. form.addComponent(firstRB);
  46. finalRadioButtonsecondRB=newRadioButton("SecondRadioButton");
  47. form.addComponent(secondRB);
  48. finalRadioButtonthirdRB=newRadioButton("ThirdRadioButton");
  49. form.addComponent(thirdRB);
  50. ActionListenerlistener=newActionListener(){
  51. //这里是处理CheckBox、RadioButton点击事件处理程序
  52. publicvoidactionPerformed(ActionEventarg0){
  53. Objectsource=arg0.getSource();
  54. if(source==firstCB)
  55. Dialog.show("Rb_Cb_Lb","firstCB","OK",null);
  56. elseif(source==secondCB)
  57. Dialog.show("Rb_Cb_Lb","secondCB","OK",null);
  58. elseif(source==firstRB)
  59. Dialog.show("Rb_Cb_Lb","firstRB","OK",null);
  60. elseif(source==secondRB)
  61. Dialog.show("Rb_Cb_Lb","secondRB","OK",null);
  62. elseif(source==thirdRB)
  63. Dialog.show("Rb_Cb_Lb","thirdRB","OK",null);
  64. }
  65. };
  66. //往ButtonGroup加入radiobutton
  67. group.add(firstRB);
  68. group.add(secondRB);
  69. group.add(thirdRB);
  70. firstRB.addActionListener(listener);//加入事件监听
  71. secondRB.addActionListener(listener);//加入事件监听
  72. thirdRB.addActionListener(listener);//加入事件监听
  73. //----为复选框加入事件监听
  74. firstCB.addActionListener(listener);
  75. secondCB.addActionListener(listener);
  76. };
  77. publicvoidactionPerformed(ActionEventarg0){
  78. //这里处理Command以及判断ButtonGroup所选中的RadioButton
  79. Commandcmd=arg0.getCommand();
  80. if(cmd==backCommand)
  81. UIDemoMIDlet.backToMainMenu();
  82. elseif(cmd==selectCommand)
  83. {
  84. Stringstr="ButtonCount:"+group.getButtonCount()+'\n'+
  85. "SelectedIndex:"+group.getSelectedIndex()+'\n'+
  86. "RadioButton:"+(group.getRadioButton(group.getSelectedIndex()).getText());
  87. Dialog.show("Rb_Cb_Lb",str,"OK",null);
  88. }
  89. }
  90. }


你可能感兴趣的:(.net,J2SE,Blog,sun)