分享

解读 LWUIT 之四:LWUIT 控件(中)

 CevenCheng 2010-11-08

解读 LWUIT 之四:LWUIT 控件(中)

LWUIT 开发指南下载
作者写的 Hello RadioButton、Hello CheckBox、Hello ComboBox 源代码下载
        本文继上篇博客继续对 LWUIT 常见控件进行介绍。本文主要介绍 LWUIT 常用控件单选按钮(RadioButton)、复选按钮(CheckBox)、组合按钮(ComboBox)的使用并附源代码。文章对这三种控件分别进行了说明和比较。
        注:源码编写中关于 .res 的编写这里不再赘述,详细编写步骤请参考作者的前一篇博客《解读 LWUIT 之二:关于 LWUIT 开发指南中的 Hello World》。每个项目的 .res 具体配置请到作者上传源码中的 res 目录下使用 ResourceEdit 查看。

com.sun.lwuit.RadioButton 控件
        RadioButton 必须和 ButtonGroup 配合才能使用。ButtonGroup 管理一组选中和未选中的单选按钮的组件,一次只产生一个选中的按钮,通过调用它的 getRadioButton(int index) 方法获得一个指定的单选按钮,而通过调用它的 getSelectedIndex() 方法获得被选中的按钮的索引。作者写的 HelloRadioButton 源代码如下:

  1. package com.defonds.lwuit;  
  2.   
  3. import com.sun.lwuit.ButtonGroup;  
  4. import com.sun.lwuit.Command;  
  5. import com.sun.lwuit.Display;  
  6. import com.sun.lwuit.Form;  
  7. import com.sun.lwuit.Label;  
  8. import com.sun.lwuit.RadioButton;  
  9. import com.sun.lwuit.animations.CommonTransitions;  
  10. import com.sun.lwuit.events.ActionEvent;  
  11. import com.sun.lwuit.events.ActionListener;  
  12. import com.sun.lwuit.layouts.BoxLayout;  
  13. import com.sun.lwuit.plaf.UIManager;  
  14. import com.sun.lwuit.util.Resources;  
  15.   
  16. public class HelloMidlet extends javax.microedition.midlet.MIDlet implements ActionListener{  
  17.       
  18.     private Form exampleContainer;// declare a Form  
  19.     private ButtonGroup group1;//declare a ButtonGroup  
  20.     private RadioButton selectedButton = new RadioButton();//remember the selected radioButton  
  21.     private RadioButton rb1;//declare a ButtonGroup  
  22.     private RadioButton rb2;//declare a ButtonGroup  
  23.     private Label radioButtonsLabel;//declare a Label  
  24.       
  25.     public void startApp() {  
  26.         // init the LWUIT Display  
  27.         Display.init(this);  
  28.         // Setting the application theme is discussed  
  29.         // later in the theme chapter and the resources chapter  
  30.         try {  
  31.             Resources r = Resources.open("/myresources.res");  
  32.             UIManager.getInstance().setThemeProps(r.getTheme("myresources"));  
  33.         } catch (java.io.IOException e) {}  
  34.           
  35.         exampleContainer = new Form("Form Title");// Create a Form;//Create a Form  
  36.         group1 = new ButtonGroup();//Create a ButtonGroup  
  37.         rb1 = new RadioButton("First RadioButton in Group 1");//Create a ButtonGroup  
  38.         rb2 = new RadioButton("Second RadioButton in Group 1");//Create a ButtonGroup  
  39.         radioButtonsLabel = new Label("RadioButton:");//Create a Label  
  40.           
  41.         rb1.addActionListener(this);//add a listener to the button  
  42.         rb2.addActionListener(this);//add a listener to the button  
  43.           
  44.         group1.add(rb1);//add rb1 to group1  
  45.         group1.add(rb2);//add rb2 to group1  
  46.           
  47.         exampleContainer.setLayout(new BoxLayout(BoxLayout.Y_AXIS));//Set LayoutManager  
  48.         exampleContainer.addComponent(rb1);//Add a RadioButton to the Form content pane  
  49.         exampleContainer.addComponent(rb2);//Add a RadioButton to the Form content pane  
  50.         exampleContainer.addComponent(radioButtonsLabel);//Add a Label to the Form content pane  
  51.         exampleContainer.setTransitionOutAnimator(CommonTransitions.createFade(400));//Set Transitions animation of Fade  
  52.         exampleContainer.addCommand(new Command("Run"2));//Add Command key  
  53.         exampleContainer.show();//Show it  
  54.     }  
  55.   
  56.     public void pauseApp() {}  
  57.   
  58.     public void destroyApp(boolean unconditional) {}  
  59.       
  60.     //implements the method of ActionListener  
  61.     public void actionPerformed(ActionEvent evt) {  
  62.         selectedButton = group1.getRadioButton(group1.getSelectedIndex());//get the radioButton which is selected by the user  
  63.         System.out.println(selectedButton.getText());  
  64.         radioButtonsLabel.setText("RadioButton:" + selectedButton.getText());//reset the text of the label  
  65.     }  
  66. }  
 
        HelloRadioButton 运行效果图如下(作者毕竟美术功底有限): 

HelloRadioButton 运行效果图

com.sun.lwuit.CheckBox 控件
        CheckBox 类似于 Html 表单中的 CheckBox,不同的是 Html 中的 CheckBox 多个一起提交时名字一样,而 LWUIT 中的 CheckBox 自行管理,需要程序员分别把结果组织。HelloCheckBox 源代码如下:

  1. package com.defonds.lwuit;  
  2.   
  3. import com.sun.lwuit.CheckBox;  
  4. import com.sun.lwuit.Command;  
  5. import com.sun.lwuit.Display;  
  6. import com.sun.lwuit.Form;  
  7. import com.sun.lwuit.Label;  
  8. import com.sun.lwuit.animations.CommonTransitions;  
  9. import com.sun.lwuit.events.ActionEvent;  
  10. import com.sun.lwuit.events.ActionListener;  
  11. import com.sun.lwuit.layouts.BoxLayout;  
  12. import com.sun.lwuit.plaf.UIManager;  
  13. import com.sun.lwuit.util.Resources;  
  14.   
  15. public class HelloMidlet extends javax.microedition.midlet.MIDlet implements ActionListener{  
  16.       
  17.     private Form exampleContainer;// declare a Form  
  18.     private CheckBox checkBox1;//declare a CheckBox  
  19.     private CheckBox checkBox2;//declare a CheckBox  
  20.     private CheckBox checkBox3;//declare a CheckBox  
  21.     private Label checkBoxLabel;//declare a Label  
  22.       
  23.     private String str = new String(" ");//a String used to remember the checkBox selected  
  24.       
  25.     public void startApp() {  
  26.         // init the LWUIT Display  
  27.         Display.init(this);  
  28.         // Setting the application theme is discussed  
  29.         // later in the theme chapter and the resources chapter  
  30.         try {  
  31.             Resources r = Resources.open("/myresources.res");  
  32.             UIManager.getInstance().setThemeProps(r.getTheme("myresources"));  
  33.         } catch (java.io.IOException e) {}  
  34.           
  35.         exampleContainer = new Form("Form Title");// Create a Form  
  36.         checkBox1 = new CheckBox("Check Box1");//Create a CheckBox  
  37.         checkBox2 = new CheckBox("Check Box2");//Create a CheckBox  
  38.         checkBox3 = new CheckBox("Check Box3");//Create a CheckBox  
  39.         checkBoxLabel = new Label("selected");//Create a Label  
  40.           
  41.         checkBox1.addActionListener(this);//add a listener to the CheckBox  
  42.         checkBox2.addActionListener(this);//add a listener to the CheckBox  
  43.         checkBox3.addActionListener(this);//add a listener to the CheckBox  
  44.           
  45.         exampleContainer.setLayout(new BoxLayout(BoxLayout.Y_AXIS));//Set LayoutManager  
  46.         exampleContainer.addComponent(checkBox1);//Add a CheckBox to the Form content pane  
  47.         exampleContainer.addComponent(checkBox2);//Add a CheckBox to the Form content pane  
  48.         exampleContainer.addComponent(checkBox3);//Add a CheckBox to the Form content pane  
  49.         exampleContainer.addComponent(checkBoxLabel);//Add a Label to the Form content pane  
  50.         exampleContainer.setTransitionOutAnimator(CommonTransitions.createFade(400));//Set Transitions animation of Fade  
  51.         exampleContainer.addCommand(new Command("Run"2));//Add Command key  
  52.         exampleContainer.show();//Show it  
  53.     }  
  54.   
  55.     public void pauseApp() {}  
  56.   
  57.     public void destroyApp(boolean unconditional) {}  
  58.       
  59.     //implements the method of ActionListener  
  60.     public void actionPerformed(ActionEvent evt) {  
  61.         if(checkBox1.isSelected()){  
  62.             str = "1,";  
  63.         }  
  64.         if(checkBox2.isSelected()){  
  65.             str += "2,";  
  66.         }  
  67.         if(checkBox3.isSelected()){  
  68.             str += "3,";  
  69.         }  
  70.         checkBoxLabel.setText(str + "selected");//reset the text of the label  
  71.         str = " ";  
  72.     }  
  73. }  
 
        HelloCheckBox 运行效果图如下:

HelloCheckBox 运行效果图

com.sun.lwuit.ComboBox 控件
        ComboBox 类似于 RadioButton,也是一次只能有一个选择的列表。但是 ComboBox 可以自行使用,并不依赖于 ButtonGroup。由于 ComboBox 可以自行定义呈现器,所以它可以不必像 RadioButton 似的占用太多空间。因此当显示空间有限,或者有多组选项的时候,ComboBox 更合适。作者写的 HelloComboBox 源代码如下:

  1. package com.defonds.lwuit;  
  2.   
  3. import com.sun.lwuit.CheckBox;  
  4. import com.sun.lwuit.ComboBox;  
  5. import com.sun.lwuit.Command;  
  6. import com.sun.lwuit.Component;  
  7. import com.sun.lwuit.Display;  
  8. import com.sun.lwuit.Form;  
  9. import com.sun.lwuit.Label;  
  10. import com.sun.lwuit.List;  
  11. import com.sun.lwuit.animations.CommonTransitions;  
  12. import com.sun.lwuit.events.ActionEvent;  
  13. import com.sun.lwuit.events.ActionListener;  
  14. import com.sun.lwuit.layouts.BoxLayout;  
  15. import com.sun.lwuit.list.ListCellRenderer;  
  16. import com.sun.lwuit.plaf.UIManager;  
  17. import com.sun.lwuit.util.Resources;  
  18.   
  19. public class HelloMidlet extends javax.microedition.midlet.MIDlet implements ActionListener{  
  20.       
  21.     private Form exampleContainer;// declare a Form  
  22.     private Label comboBoxLabel;//declare a Label  
  23.     private String[] content = { "Red""Blue""Green""Yellow" };  
  24.     private ComboBox comboBox;//declare a ComboBox  
  25.       
  26.     public void startApp() {  
  27.         // init the LWUIT Display  
  28.         Display.init(this);  
  29.         // Setting the application theme is discussed  
  30.         // later in the theme chapter and the resources chapter  
  31.         try {  
  32.             Resources r = Resources.open("/myresources.res");  
  33.             UIManager.getInstance().setThemeProps(r.getTheme("myresources"));  
  34.         } catch (java.io.IOException e) {}  
  35.           
  36.         exampleContainer = new Form("Form Title");// Create a Form  
  37.         comboBoxLabel = new Label(" ");  
  38.         comboBox = new ComboBox(content);//Creating the combo box  
  39.         comboBox.setListCellRenderer(new checkBoxRenderer());//Setting a checkBox renderer  
  40.         comboBox.addActionListener(this);//Adding a action listener to catch user clicking  
  41.           
  42.         exampleContainer.setLayout(new BoxLayout(BoxLayout.Y_AXIS));//Set LayoutManager  
  43.         exampleContainer.addComponent(comboBox);//Add a ComboBox to the Form content pane  
  44.         exampleContainer.addComponent(comboBoxLabel);//Add a Label to the Form content pane  
  45.         exampleContainer.setTransitionOutAnimator(CommonTransitions.createFade(400));//Set Transitions animation of Fade  
  46.         exampleContainer.addCommand(new Command("Run"2));//Add Command key  
  47.         exampleContainer.show();//Show it  
  48.     }  
  49.   
  50.     public void pauseApp() {}  
  51.   
  52.     public void destroyApp(boolean unconditional) {}  
  53.       
  54.     //implements the method of ActionListener  
  55.     public void actionPerformed(ActionEvent evt) {  
  56.         comboBoxLabel.setText(content[comboBox.getSelectedIndex()] + " selected");  
  57.     }  
  58.       
  59.     /** 
  60.     * Demonstrates implementation of a renderer derived from a CheckBox 
  61.     */  
  62.     private static class checkBoxRenderer extends CheckBox implements ListCellRenderer{  
  63.         /** Creates a new instance of checkBoxRenderer */  
  64.         public checkBoxRenderer(){  
  65.             super("");  
  66.         }  
  67.           
  68.         // Setting the current check box text and status  
  69.         public Component getListCellRendererComponent(List list,  
  70.                 Object value, int index, boolean isSelected) {  
  71.             setText("" + value);  
  72.             if (isSelected){  
  73.                 setFocus(true);  
  74.                 setSelected(true);  
  75.             }else{  
  76.                 setFocus(false);  
  77.                 setSelected(false);  
  78.             }  
  79.             return this;  
  80.         }  
  81.           
  82.         // Returning the list focus component  
  83.         public Component getListFocusComponent(List list) {  
  84.             setText("");  
  85.             setFocus(true);  
  86.             setSelected(true);  
  87.             return this;  
  88.         }  
  89.     }  
  90. }  
 
        HelloComboBox 运行效果图如下:

HelloComboBox 运行效果图

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多