import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton; //=================================================
// File Name : JCheckBox_demo
//------------------------------------------------------------------------------
// Author : Common // 类名:JCheckBox
// 属性:
// 方法:
class MyCheckBox implements ItemListener{
private JFrame frame = new JFrame("窗体");
private Container cont = frame.getContentPane();
private JCheckBox jrb1 = new JCheckBox("单选1");
private JCheckBox jrb2 = new JCheckBox("单选2");
private JCheckBox jrb3 = new JCheckBox("单选3");
private JPanel pan = new JPanel();
public MyCheckBox(){
//定义一个面板的边框显示条
pan.setBorder(BorderFactory.createTitledBorder("请选择按钮"));
pan.setLayout(new GridLayout(1,3)); pan.add(this.jrb1);
pan.add(this.jrb2);
pan.add(this.jrb3); jrb1.addItemListener(this); //加入事件监听
jrb2.addItemListener(this); //加入事件监听
jrb3.addItemListener(this); //加入事件监听 cont.add(pan);
this.frame.setSize(330,80);
this.frame.setVisible(true);
this.frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) { //窗口关闭时触发,按下关闭按钮
// TODO 自动生成的方法存根
System.out.println("windowClosing-->窗口关闭");
System.exit(1);
}
});
}
@Override
public void itemStateChanged(ItemEvent e) {
// TODO 自动生成的方法存根
if(jrb1.isSelected()){
System.out.println("jrb1");
}
if(jrb2.isSelected()){
System.out.println("jrb2");
}
if(jrb3.isSelected()){
System.out.println("jrb3");
}
}
} //主类
//Function : JCheckBox_demo
public class JCheckBox_demo { public static void main(String[] args) {
// TODO 自动生成的方法存根
new MyCheckBox();
} }