java awt_Java AWT CheckboxGroup

java awt

We have seen in previous sections what a Checkbox class is. When we have a group of checkboxes, we can select as many as we want from them, However, in case we want to restrict the user so that he can choose a maximum of 1 option from a set, we use the CheckboxGroup class. It is synonymous with the more frequently used Radio Button.

我们已经在前面的章节中看到了什么是Checkbox类。 当我们有一组复选框时 ,我们可以从中选择任意数量的复选框 ,但是,如果要限制用户以便他可以从一组中选择最多1个选项,则可以使用CheckboxGroup类 。 它与更常用的单选按钮同义。

The AWT package does not have the option to render a radio button. Thus a CheckboxGroup object is used.

AWT程序包没有呈现单选按钮的选项。 因此,使用了CheckboxGroup对象。

A CheckboxGroup object is used to form a set of checkboxes, out of which only one can be in the on state and all others must be in the off state.

CheckboxGroup对象用于形成一组复选框,其中只有一个复选框可以处于打开状态,而所有其他复选框必须处于关闭状态。

A CheckboxGroup object creates an ItemEvent whenever the state of one of the checkboxes changes. We can use the ItemListener interface to handle events fired by this class. We shall learn more about dealing with events in later sections.

每当复选框之一的状态更改时, CheckboxGroup对象都会创建一个ItemEvent。 我们可以使用ItemListener接口来处理此类触发的事件。 我们将在后面的部分中了解有关处理事件的更多信息。

Consider the following code -

考虑以下代码-

import java.awt.*;
import javax.swing.*;

public class CreateCheckboxGroup {

    CreateCheckboxGroup() {
        Frame f = new Frame();

        CheckboxGroup fruits = new CheckboxGroup();
        CheckboxGroup cars = new CheckboxGroup();

        Checkbox c1 = new Checkbox("Apple", fruits, false);
        Checkbox c2 = new Checkbox("Mango", fruits, false);
        Checkbox c3 = new Checkbox("Banana", fruits, true);

        Checkbox c4 = new Checkbox("BMW", cars, false);
        Checkbox c5 = new Checkbox("Merc", cars, false);

        f.setLayout(new BoxLayout(f, BoxLayout.Y_AXIS));
        f.setVisible(true);
        f.setSize(300, 300);

        f.add(c1);
        f.add(c2);
        f.add(c3);
        f.add(c4);
        f.add(c5);
    }

    public static void main(String[] args) {
        CreateCheckboxGroup ob = new CreateCheckboxGroup();
    }
}

Output

输出量

Java AWT CheckboxGroup




Java AWT CheckboxGroup

In the output, the first picture depicts the initial view of the application. As can be seen, banana is selected from one group and no choice has been selected from the other group. The second picture depicts the state after the user interacts with the application, by selecting Mango from fruits and Merc from cars.

在输出中,第一张图片描绘了应用程序的初始视图。 可以看出,香蕉是从一个组中选择的,而没有选择是从另一组中选择的。 第二张图片描绘了用户与应用程序交互后的状态,方法是从水果中选择芒果 ,从汽车中选择Merc

In the code, we have created two objects of the CheckboxGroup class. These two sets, namely "fruits" and "cars" hold 3 and 2 checkboxes respectively. Each checkbox interacts with the user in the same way as any usual Checkbox class object. However, the key difference being that only one checkbox can be selected to be true from a set. Whenever the user changes his option, all other checkboxes get automatically unchecked.

在代码中,我们创建了CheckboxGroup类的两个对象。 这两个集合(即“水果”“汽车”)分别包含3个和2个复选框。 每个复选框都以与任何常规Checkbox类对象相同的方式与用户交互。 但是,关键的区别是从一个集合中只能选择一个复选框为true。 每当用户更改其选项时,所有其他复选框都会自动取消选中。

In the "fruits" set, we have set c3 object to be checked by default. This means that all other checkboxes of this group will be unchecked initially, except for this checkbox.

“水果”集中,我们将c3对象设置为默认检查。 这意味着除此复选框外,该组的所有其他复选框最初将被取消选中。

In the "cars" set, we have created two checkboxes, c4 and c5. Both objects are unchecked by default. The user has the ability to choose any one option from these two and also the freedom to change his selected option at any point in time.

“汽车”集中,我们创建了两个复选框c4c5 。 默认情况下,两个对象均未选中。 用户可以从这两个选项中选择一个,也可以随时更改其选择的选项。

To add any checkbox to a checkbox group, we use the constructor as shown in the code.
Checkbox(String name, CheckboxGroup cbg, boolean state)

要将任何复选框添加到复选框组,我们使用代码中所示的构造函数。
Checkbox( 字符串名称,CheckboxGroup cbg,布尔状态 )

翻译自: https://www.includehelp.com/java/awt-checkboxgroup.aspx

java awt

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值