📜  Java Swing-JCheckBox

📅  最后修改于: 2020-09-30 05:56:22             🧑  作者: Mango

Java JCheckBox

JCheckBox类用于创建复选框。它用于打开(true)或关闭(false)选项。单击CheckBox会将其状态从“打开”更改为“关闭”,或从“关闭”更改为“打开”。它继承了JToggleButton类。

JCheckBox类声明

我们来看一下javax.swing.JCheckBox类的声明。

public class JCheckBox extends JToggleButton implements Accessible

常用的构造函数:

Constructor Description
JJCheckBox() Creates an initially unselected check box button with no text, no icon.
JChechBox(String s) Creates an initially unselected check box with text.
JCheckBox(String text, boolean selected) Creates a check box with text and specifies whether or not it is initially selected.
JCheckBox(Action a) Creates a check box where properties are taken from the Action supplied.

常用方法:

Methods Description
AccessibleContext getAccessibleContext() It is used to get the AccessibleContext associated with this JCheckBox.
protected String paramString() It returns a string representation of this JCheckBox.

Java JCheckBox示例

import javax.swing.*;
public class CheckBoxExample
{
 CheckBoxExample(){
    JFrame f= new JFrame("CheckBox Example");
    JCheckBox checkBox1 = new JCheckBox("C++");
    checkBox1.setBounds(100,100, 50,50);
    JCheckBox checkBox2 = new JCheckBox("Java", true);
    checkBox2.setBounds(100,150, 50,50);
    f.add(checkBox1);
    f.add(checkBox2);
    f.setSize(400,400);
    f.setLayout(null);
    f.setVisible(true);
 }
public static void main(String args[])
    {
    new CheckBoxExample();
    }}

输出:

带有ItemListener的Java JCheckBox示例

import javax.swing.*;
import java.awt.event.*;  
public class CheckBoxExample  
{  
     CheckBoxExample(){  
        JFrame f= new JFrame("CheckBox Example");  
        final JLabel label = new JLabel();          
        label.setHorizontalAlignment(JLabel.CENTER);  
        label.setSize(400,100);  
        JCheckBox checkbox1 = new JCheckBox("C++");  
        checkbox1.setBounds(150,100, 50,50);  
        JCheckBox checkbox2 = new JCheckBox("Java");  
        checkbox2.setBounds(150,150, 50,50);  
        f.add(checkbox1); f.add(checkbox2); f.add(label);  
        checkbox1.addItemListener(new ItemListener() {  
             public void itemStateChanged(ItemEvent e) {               
                label.setText("C++ Checkbox: "   
                + (e.getStateChange()==1?"checked":"unchecked"));  
             }  
          });  
        checkbox2.addItemListener(new ItemListener() {  
             public void itemStateChanged(ItemEvent e) {               
                label.setText("Java Checkbox: "   
                + (e.getStateChange()==1?"checked":"unchecked"));  
             }  
          });  
        f.setSize(400,400);  
        f.setLayout(null);  
        f.setVisible(true);  
     }  
public static void main(String args[])  
{  
    new CheckBoxExample();  
}  
}  

输出:

Java JCheckBox示例:食物订单

import javax.swing.*;
import java.awt.event.*;
public class CheckBoxExample extends JFrame implements ActionListener{
JLabel l;
JCheckBox cb1,cb2,cb3;
JButton b;
CheckBoxExample(){
l=new JLabel("Food Ordering System");
l.setBounds(50,50,300,20);
cb1=new JCheckBox("Pizza @ 100");
cb1.setBounds(100,100,150,20);
cb2=new JCheckBox("Burger @ 30");
cb2.setBounds(100,150,150,20);
cb3=new JCheckBox("Tea @ 10");
cb3.setBounds(100,200,150,20);
b=new JButton("Order");
b.setBounds(100,250,80,30);
b.addActionListener(this);
add(l);add(cb1);add(cb2);add(cb3);add(b);
setSize(400,400);
setLayout(null);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
float amount=0;
String msg="";
if(cb1.isSelected()){
amount+=100;
msg="Pizza: 100\n";
}
if(cb2.isSelected()){
amount+=30;
msg+="Burger: 30\n";
}
if(cb3.isSelected()){
amount+=10;
msg+="Tea: 10\n";
}
msg+="-----------------\n";
JOptionPane.showMessageDialog(this,msg+"Total: "+amount);
}
public static void main(String[] args) {
new CheckBoxExample();
}
}

输出: