Hello LWUIT——LWUIT开发指南3

不知道怎么搞的,今天我将翻译好的word文档复制到博客里面来,排版方面始终有点问题。现在不影响阅读,就将就到了吧。

2.6 单选按钮

    单选按钮(RadioButton)是在一个按钮组(ButtonGroup)中只能选择一个的按钮。因为单选按钮继承于按钮,所以它具有所有普通按钮的特性,这已经在2.5部分讨论过了。例如,你可以指定一张图片显示在单选按钮上。用户每次点击单选按钮时(甚至它已经被选中),这个按钮都会像按钮一样触发动作事件。
创建一个单选按钮,使用:

RadioButton radioButton = new RadioButton(“Radio Button”);

    图2-5显示了上面代码产生的单选按钮。

   

2.7 按钮组


    按钮组(ButtonGroup)是管理一组选中和未选中的单选按钮的组件。在这个组中,只能一次产生一个选中的按钮。
最先,在按钮组中的所有单选按钮都是未选中的。每一个按钮组控制选中的序号,并可以通过调用getRadioButton(int index).方法获得一个指定的单选按钮。
下面的代码段创建了由两个单选按钮组成的按钮组。   

Label radioButtonsLabel = new Label("RadioButton:");
....
RadioButton rb1 = new RadioButton("First RadioButton in Group 1");
RadioButton rb2 = new RadioButton("Second RadioButton in Group 1");
ButtonGroup group1 = new ButtonGroup();
group1.add(rb1);
group1.add(rb2);
exampleContainer.addComponent(radioButtonsLabel);
exampleContainer.addComponent(rb1);
exampleContainer.addComponent(rb2);

     图2-6显示了这个代码段产生的效果。

   

2.8 复选按钮


    复选按钮(CheckBox)与单选按钮类似,但是他们的选择模式不一样,因为复选按钮可以在选中和未选中两种模式中切换。另一方面,一组单选按钮,只有一个按钮被选中。因为多选框继承于按钮,所以它具有普通按钮的所有特性,这些在2.5部分已经讨论过。例如,你可以指定一张图片显示在复选按钮上。用户每次点击复选按钮时(甚至它已经被选中),这个按钮都会像按钮一样触发动作事件。
创建一个多选框使用:

final CheckBox checkBox = new CheckBox(“Check Box”); 

    这段代码产生的多选框如图2-7所示。

   
    通过下面的代码可以捕获到选中或者未选中的状态。   

checkBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if(checkBox.isSelected()) {
System.out.println("CheckBox got selected");
} else {
System.out.println("CheckBox got unselected");
}
}
});

 

2.9 组合按钮

    组合按钮(ComboBox)是一个一次只能有一个选择的列表。当用户点击组合按钮的时候,将出现一个包括只允许用户选择一个元素在内的多个元素的下拉列表。组合按钮起源于列表模式,并同时也具有列表的特性。
另外一些组件也具有多选一的功能,比如单选按钮,多选按钮,,按钮和列表。一组单选按钮通常是用户最容易理解的一种方式,但是当空间有限,或者有多组选项的时候,组合按钮更适合。列表通常不引人注意,但是当选项很多时,它比组合按钮更适合,比如超过5个选项。
    下面的代码段创建一个组合按钮,并对它进行设置:   

String[] content = { "Red", "Blue", "Green", "Yellow" };
// 1. Creating the combo box
ComboBox comboBox = new ComboBox(content);
// 2. Setting a checkBox renderer
comboBox.setListCellRenderer(new checkBoxRenderer());
// 3. Adding a action listener to catch user clicking
// to open the ComboBox
comboBox.addActionListener(myActionListener......); 

    下面的笔记与上面代码的注释相对应。
    1. 这个组合按钮包括一个字符串数组,你也可以简单的用标签代替。
    2. 放置其他的东西到组合按钮中,或者自定义这个组合按钮怎么显示,你需要自定义一个呈现器。
    3. 下一行代码注册一个动作监听器到这个组合按钮中。
    下面是呈现器的示例代码:   

/**
* Demonstrates implementation of a renderer derived from a CheckBox
*/
private static class checkBoxRenderer extends CheckBox implements
ListCellRenderer {
/** Creates a new instance of checkBoxRenderer */
public checkBoxRenderer() {
super("");
}
// Setting the current check box text and status
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;
}
// Returning the list focus component
// (discussed in Chapter 3)
public Component getListFocusComponent(List list) {
setText("");
setFocus(true);
setSelected(true);
return this;
}
} 

    示例代码产生的组合框如图2-8所示。

   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值