Java 二级-复选按钮

package hello;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Hello extends JFrame{
    
    private JLabel lab;
    private JCheckBox box1,box2;
    
    public  Hello(){
        super("复选按钮");
        setSize(300,300);
        setLocation(300,300);
        setLayout(new FlowLayout());
        Container ban=getContentPane();
        lab=new JLabel("you do not choose");
        box1=new JCheckBox("zhu",false);
        box2=new JCheckBox("laz",false);
        ban.add(lab);
        ban.add(box1);
        ban.add(box2);
        box1.addItemListener(new itemevent());
        box2.addItemListener(new itemevent());
        setVisible(true);
    }
    
    public static void main(String args[])
    {
        Hello tt=new Hello();
        tt.addWindowListener( new win());
        
    }
    
    class itemevent implements ItemListener{

        @Override
        public void itemStateChanged(ItemEvent e) {
            // TODO Auto-generated method stub
            if(e.getSource()==box1) {
                if(e.getStateChange()==ItemEvent.SELECTED)
                    lab.setText("you choose zhu");
                else {
                    lab.setText("you do not choose zhu");
                }
            }
            if(e.getSource()==box2) {    
                if(e.getStateChange()==ItemEvent.SELECTED) 
                    lab.setText("you choose laz");
                else {
                    lab.setText("you do not choose zhu");
                }
            
            }
        }       
    }
    
    static class win extends WindowAdapter{
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    }
    
}

你可能感兴趣的:(Java 二级-复选按钮)