import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton; //=================================================
// File Name : JRadioButton_demo
//------------------------------------------------------------------------------
// Author : Common // 类名:myWindowEventHandle
// 属性:
// 方法:
class MyRadio{
private JFrame frame = new JFrame("窗体");
private Container cont = frame.getContentPane();
private JRadioButton jrb1 = new JRadioButton("单选1");
private JRadioButton jrb2 = new JRadioButton("单选2");
private JRadioButton jrb3 = new JRadioButton("单选3");
private JPanel pan = new JPanel();
public MyRadio(){
//定义一个面板的边框显示条
pan.setBorder(BorderFactory.createTitledBorder("请选择按钮"));
pan.setLayout(new GridLayout(1,3));
ButtonGroup group = new ButtonGroup();
group.add(this.jrb1);
group.add(this.jrb2);
group.add(this.jrb3);
pan.add(this.jrb1);
pan.add(this.jrb2);
pan.add(this.jrb3);
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);
}
});
}
} //主类
//Function : JRadioButton_demo
public class JRadioButton_demo { public static void main(String[] args) {
// TODO 自动生成的方法存根
new MyRadio();
} }
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.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton; //=================================================
// File Name : JRadioButton_demo
//------------------------------------------------------------------------------
// Author : Common // 类名:myWindowEventHandle
// 属性:
// 方法:
class MyRadio implements ItemListener{
private JFrame frame = new JFrame("窗体");
private Container cont = frame.getContentPane();
private JRadioButton jrb1 = new JRadioButton("单选1");
private JRadioButton jrb2 = new JRadioButton("单选2");
private JRadioButton jrb3 = new JRadioButton("单选3");
private JPanel pan = new JPanel();
public MyRadio(){
//定义一个面板的边框显示条
pan.setBorder(BorderFactory.createTitledBorder("请选择按钮"));
pan.setLayout(new GridLayout(1,3));
ButtonGroup group = new ButtonGroup();
group.add(this.jrb1);
group.add(this.jrb2);
group.add(this.jrb3);
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(e.getSource() == jrb1){
System.out.println("jrb1");
}else if(e.getSource() == jrb2){
System.out.println("jrb2");
}else{
System.out.println("jrb3");
}
}
} //主类
//Function : JRadioButton_demo
public class JRadioButton_demo { public static void main(String[] args) {
// TODO 自动生成的方法存根
new MyRadio();
} }