解读 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 源代码如下: - package com.defonds.lwuit;
-
- import com.sun.lwuit.ButtonGroup;
- import com.sun.lwuit.Command;
- import com.sun.lwuit.Display;
- import com.sun.lwuit.Form;
- import com.sun.lwuit.Label;
- import com.sun.lwuit.RadioButton;
- import com.sun.lwuit.animations.CommonTransitions;
- import com.sun.lwuit.events.ActionEvent;
- import com.sun.lwuit.events.ActionListener;
- import com.sun.lwuit.layouts.BoxLayout;
- import com.sun.lwuit.plaf.UIManager;
- import com.sun.lwuit.util.Resources;
-
- public class HelloMidlet extends javax.microedition.midlet.MIDlet implements ActionListener{
-
- private Form exampleContainer;
- private ButtonGroup group1;
- private RadioButton selectedButton = new RadioButton();
- private RadioButton rb1;
- private RadioButton rb2;
- private Label radioButtonsLabel;
-
- public void startApp() {
-
- Display.init(this);
-
-
- try {
- Resources r = Resources.open("/myresources.res");
- UIManager.getInstance().setThemeProps(r.getTheme("myresources"));
- } catch (java.io.IOException e) {}
-
- exampleContainer = new Form("Form Title");
- group1 = new ButtonGroup();
- rb1 = new RadioButton("First RadioButton in Group 1");
- rb2 = new RadioButton("Second RadioButton in Group 1");
- radioButtonsLabel = new Label("RadioButton:");
-
- rb1.addActionListener(this);
- rb2.addActionListener(this);
-
- group1.add(rb1);
- group1.add(rb2);
-
- exampleContainer.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
- exampleContainer.addComponent(rb1);
- exampleContainer.addComponent(rb2);
- exampleContainer.addComponent(radioButtonsLabel);
- exampleContainer.setTransitionOutAnimator(CommonTransitions.createFade(400));
- exampleContainer.addCommand(new Command("Run", 2));
- exampleContainer.show();
- }
-
- public void pauseApp() {}
-
- public void destroyApp(boolean unconditional) {}
-
-
- public void actionPerformed(ActionEvent evt) {
- selectedButton = group1.getRadioButton(group1.getSelectedIndex());
- System.out.println(selectedButton.getText());
- radioButtonsLabel.setText("RadioButton:" + selectedButton.getText());
- }
- }
HelloRadioButton 运行效果图如下(作者毕竟美术功底有限): 
com.sun.lwuit.CheckBox 控件 CheckBox 类似于 Html 表单中的 CheckBox,不同的是 Html 中的 CheckBox 多个一起提交时名字一样,而 LWUIT 中的 CheckBox 自行管理,需要程序员分别把结果组织。HelloCheckBox 源代码如下: - package com.defonds.lwuit;
-
- import com.sun.lwuit.CheckBox;
- import com.sun.lwuit.Command;
- import com.sun.lwuit.Display;
- import com.sun.lwuit.Form;
- import com.sun.lwuit.Label;
- import com.sun.lwuit.animations.CommonTransitions;
- import com.sun.lwuit.events.ActionEvent;
- import com.sun.lwuit.events.ActionListener;
- import com.sun.lwuit.layouts.BoxLayout;
- import com.sun.lwuit.plaf.UIManager;
- import com.sun.lwuit.util.Resources;
-
- public class HelloMidlet extends javax.microedition.midlet.MIDlet implements ActionListener{
-
- private Form exampleContainer;
- private CheckBox checkBox1;
- private CheckBox checkBox2;
- private CheckBox checkBox3;
- private Label checkBoxLabel;
-
- private String str = new String(" ");
-
- public void startApp() {
-
- Display.init(this);
-
-
- try {
- Resources r = Resources.open("/myresources.res");
- UIManager.getInstance().setThemeProps(r.getTheme("myresources"));
- } catch (java.io.IOException e) {}
-
- exampleContainer = new Form("Form Title");
- checkBox1 = new CheckBox("Check Box1");
- checkBox2 = new CheckBox("Check Box2");
- checkBox3 = new CheckBox("Check Box3");
- checkBoxLabel = new Label("selected");
-
- checkBox1.addActionListener(this);
- checkBox2.addActionListener(this);
- checkBox3.addActionListener(this);
-
- exampleContainer.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
- exampleContainer.addComponent(checkBox1);
- exampleContainer.addComponent(checkBox2);
- exampleContainer.addComponent(checkBox3);
- exampleContainer.addComponent(checkBoxLabel);
- exampleContainer.setTransitionOutAnimator(CommonTransitions.createFade(400));
- exampleContainer.addCommand(new Command("Run", 2));
- exampleContainer.show();
- }
-
- public void pauseApp() {}
-
- public void destroyApp(boolean unconditional) {}
-
-
- public void actionPerformed(ActionEvent evt) {
- if(checkBox1.isSelected()){
- str = "1,";
- }
- if(checkBox2.isSelected()){
- str += "2,";
- }
- if(checkBox3.isSelected()){
- str += "3,";
- }
- checkBoxLabel.setText(str + "selected");
- str = " ";
- }
- }
HelloCheckBox 运行效果图如下:
com.sun.lwuit.ComboBox 控件 ComboBox 类似于 RadioButton,也是一次只能有一个选择的列表。但是 ComboBox 可以自行使用,并不依赖于 ButtonGroup。由于 ComboBox 可以自行定义呈现器,所以它可以不必像 RadioButton 似的占用太多空间。因此当显示空间有限,或者有多组选项的时候,ComboBox 更合适。作者写的 HelloComboBox 源代码如下: - package com.defonds.lwuit;
-
- import com.sun.lwuit.CheckBox;
- import com.sun.lwuit.ComboBox;
- import com.sun.lwuit.Command;
- import com.sun.lwuit.Component;
- import com.sun.lwuit.Display;
- import com.sun.lwuit.Form;
- import com.sun.lwuit.Label;
- import com.sun.lwuit.List;
- import com.sun.lwuit.animations.CommonTransitions;
- import com.sun.lwuit.events.ActionEvent;
- import com.sun.lwuit.events.ActionListener;
- import com.sun.lwuit.layouts.BoxLayout;
- import com.sun.lwuit.list.ListCellRenderer;
- import com.sun.lwuit.plaf.UIManager;
- import com.sun.lwuit.util.Resources;
-
- public class HelloMidlet extends javax.microedition.midlet.MIDlet implements ActionListener{
-
- private Form exampleContainer;
- private Label comboBoxLabel;
- private String[] content = { "Red", "Blue", "Green", "Yellow" };
- private ComboBox comboBox;
-
- public void startApp() {
-
- Display.init(this);
-
-
- try {
- Resources r = Resources.open("/myresources.res");
- UIManager.getInstance().setThemeProps(r.getTheme("myresources"));
- } catch (java.io.IOException e) {}
-
- exampleContainer = new Form("Form Title");
- comboBoxLabel = new Label(" ");
- comboBox = new ComboBox(content);
- comboBox.setListCellRenderer(new checkBoxRenderer());
- comboBox.addActionListener(this);
-
- exampleContainer.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
- exampleContainer.addComponent(comboBox);
- exampleContainer.addComponent(comboBoxLabel);
- exampleContainer.setTransitionOutAnimator(CommonTransitions.createFade(400));
- exampleContainer.addCommand(new Command("Run", 2));
- exampleContainer.show();
- }
-
- public void pauseApp() {}
-
- public void destroyApp(boolean unconditional) {}
-
-
- public void actionPerformed(ActionEvent evt) {
- comboBoxLabel.setText(content[comboBox.getSelectedIndex()] + " selected");
- }
-
-
-
-
- private static class checkBoxRenderer extends CheckBox implements ListCellRenderer{
-
- public checkBoxRenderer(){
- super("");
- }
-
-
- public Component getListCellRendererComponent(List list,
- Object value, int index, boolean isSelected) {
- setText("" + value);
- if (isSelected){
- setFocus(true);
- setSelected(true);
- }else{
- setFocus(false);
- setSelected(false);
- }
- return this;
- }
-
-
- public Component getListFocusComponent(List list) {
- setText("");
- setFocus(true);
- setSelected(true);
- return this;
- }
- }
- }
HelloComboBox 运行效果图如下:
|