java awt 选择框_Java AWT复选框

java awt 选择框

The checkbox is a GUI component that is used to create a checkbox control. It contains a box that can be checked or unchecked by clicking on it. It is used to turn the state from "on" or "off" or from "off" to "on". It extends the Component class and can be added to containers like Frame and Panels.

复选框是一个GUI组件,用于创建复选框控件。 它包含一个可以通过单击选中或取消选中的框。 它用于将状态从“打开”或“关闭”或从“关闭”更改为“打开”。 它扩展了Component类,可以添加到诸如Frame和Panels的容器中。

Whenever a checkbox is clicked, it generates an event as its state changes. We can handle such events using the ItemListener interface. Any class implementing the ItemListener interface can handle events generated by a checkbox. On being clicked, an ItemEvent is fired and the corresponding ItemListener called. We will study the details of handling events in later sections.

每当单击复选框时,其状态更改都会生成一个事件。 我们可以使用ItemListener接口处理此类事件。 任何实现ItemListener接口的类都可以处理复选框生成的事件。 单击后,将触发ItemEvent并调用相应的ItemListener 。 我们将在后面的部分中研究处理事件的详细信息。

Consider the following code -

考虑以下代码-

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

public class CreateCheckBox{

     CreateCheckBox()
     {
         Frame f = new Frame();
         Checkbox c1 = new Checkbox();
         Checkbox c2 = new Checkbox("C++");
         Checkbox c3 = new Checkbox("Java", true);
         
         f.setLayout(new BoxLayout(f, BoxLayout.Y_AXIS));
         f.setVisible(true);
         f.setSize(300,300);
         
         if(c3.getState() == true)
         c1.setLabel("Label Changed");
         
         
         f.add(c1);
         f.add(c2);
         f.add(c3);
     
     }
                  
        public static void main(String []args){
        CreateCheckBox ob = new CreateCheckBox();
        
     }
}

Output

输出量

Java AWT Checkbox

As can be seen in the code, we created 3 instances of Checkbox class. C1 is initialized without any label. C2 has a label with the same name that is passed as an argument at the time of object initialization. By default, this checkbox is unchecked. If we want the checkbox to be "checked" by default, we can create an object the same way as in c3. The second parameter is a boolean value which decides the state of the checkbox.

从代码中可以看出,我们创建了3个Checkbox类实例。 C1初始化时没有任何标签。 C2具有与对象初始化时作为参数传递的名称相同的标签。 默认情况下,未选中此复选框。 如果我们希望默认情况下选中该复选框,则可以使用与c3中相同的方式创建一个对象。 第二个参数是一个布尔值,它决定复选框的状态。

We can retrieve the current state of a checkbox using the getState() method. It returns a boolean value – false indicating that the box in unchecked true indicating that the box is checked.

我们可以使用getState()方法检索复选框的当前状态。 它返回一个布尔值– false表示该框处于未选中状态; true表示该框处于选中状态。

Not only the user has the capability of changing the state of the checkbox, but even the programmer can change the state using the setState() function. For eg., if we call c1.setState(true) it would "check" the checkbox of c1 object.

不仅用户具有更改复选框状态的能力,甚至程序员也可以使用setState()函数来更改状态。 例如,如果调用c1.setState(true) ,它将“选中” c1对象的复选框。

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

java awt 选择框

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值