单选组件JRadioButton
源代码:
<span style="font-size:18px;">//source code import java.awt.Container ; import java.awt.GridLayout ; import java.awt.Font ; import java.awt.event.WindowListener ; import java.awt.event.WindowEvent ; import java.awt.event.WindowAdapter ; import javax.swing.JFrame ; import javax.swing.JRadioButton ; import javax.swing.ButtonGroup ; import javax.swing.JPanel ; import javax.swing.BorderFactory ; class MyJRadio { private JFrame frame = new JFrame("调查问卷") ; Container con = frame.getContentPane() ; Font fnt = new Font("楷体",Font.BOLD,20) ; private JRadioButton jrb1 = new JRadioButton("你妹") ; private JRadioButton jrb2 = new JRadioButton("尼玛") ; private JRadioButton jrb3 = new JRadioButton("卧槽") ; private JPanel pan = new JPanel() ; public MyJRadio() { String title = "你是逗比吗?" ; pan.setBorder(BorderFactory.createTitledBorder(title)) ; pan.setLayout(new GridLayout(1,3)) ; jrb1.setFont(fnt) ; jrb2.setFont(fnt) ; jrb3.setFont(fnt) ; pan.add(this.jrb1) ; pan.add(this.jrb2) ; pan.add(this.jrb3) ; ButtonGroup group = new ButtonGroup() ; //将所有的JRadioButton加入ButtonGroup确保单选 group.add(jrb1) ; group.add(jrb2) ; group.add(jrb3) ; con.add(pan) ; this.frame.setSize(400,100) ; this.frame.setVisible(true) ; this.frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(1) ; } }) ; } } ; class Tester { public static void main(String args[]) { MyJRadio mjr = new MyJRadio() ; } } ; </span>