1. import javax.swing.JFrame;
2. import javax.swing.JRadioButton;
3. import javax.swing.JLabel;
4. import javax.swing.BoxLayout;
5. import javax.swing.ButtonGroup;
6. import java.awt.Container;
7. import java.awt.event.ActionListener;
8. import java.awt.event.ActionEvent;
9. import java.awt.event.WindowEvent;
10. import java.awt.event.WindowAdapter;
11.
12. public class RadioDemo extends JFrame implements ActionListener {
13. JLabel label;
14. JRadioButton buttonA;
15. JRadioButton buttonB;
16. JRadioButton buttonC;
17. JRadioButton buttonD;
18. public static void main(String arg[]) {
19. new RadioDemo();
20. }
21. public RadioDemo() {
22. addWindowListener(new WindowAdapter() {
23. public void windowClosing(WindowEvent e) {
24. System.exit(0);
25. }
26. } );
27. setLocation(250,150);
28. Container pane = getContentPane();
29. pane.setLayout(new BoxLayout(pane,BoxLayout.Y_AXIS));
30.
31. ButtonGroup group = new ButtonGroup();//单选按钮分组以保证单一选择
32.
33. label = new JLabel("Ajava.org");
34. pane.add(label);
35.
36. buttonA = new JRadioButton("Aardvark");
37. buttonA.setSelected(true);
38. buttonA.addActionListener(this);
39. group.add(buttonA);
40. pane.add(buttonA);
41.
42. buttonB = new JRadioButton("Baracuda");
43. buttonB.setSelected(false);
44. buttonB.addActionListener(this);
45. group.add(buttonB);
46. pane.add(buttonB);
47.
48. buttonC = new JRadioButton("Condor");
49. buttonC.setSelected(false);
50. buttonC.addActionListener(this);
51. group.add(buttonC);
52. pane.add(buttonC);
53.
54. buttonD = new JRadioButton("Daryll");
55. buttonD.setSelected(false);
56. buttonD.addActionListener(this);
57. group.add(buttonD);
58. pane.add(buttonD);
59.
60. pack();
61. setVisible(true);
62. }
63. public void actionPerformed(ActionEvent e) {
64. String text = "";
65. if(buttonA.isSelected())
66. text = "A";
67. else if(buttonB.isSelected())
68. text = "B";
69. else if(buttonC.isSelected())
70. text = "C";
71. else if(buttonD.isSelected())
72. text = "D";
73. label.setText(text);
74. Object obj = e.getSource();
75. if(obj == buttonA)
76. System.out.println("buttonA");
77. if(obj == buttonB)
78. System.out.println("buttonB");
79. if(obj == buttonC)
80. System.out.println("buttonC");
81. if(obj == buttonD)
82. System.out.println("buttonD");
83. }
84. }
2. import javax.swing.JRadioButton;
3. import javax.swing.JLabel;
4. import javax.swing.BoxLayout;
5. import javax.swing.ButtonGroup;
6. import java.awt.Container;
7. import java.awt.event.ActionListener;
8. import java.awt.event.ActionEvent;
9. import java.awt.event.WindowEvent;
10. import java.awt.event.WindowAdapter;
11.
12. public class RadioDemo extends JFrame implements ActionListener {
13. JLabel label;
14. JRadioButton buttonA;
15. JRadioButton buttonB;
16. JRadioButton buttonC;
17. JRadioButton buttonD;
18. public static void main(String arg[]) {
19. new RadioDemo();
20. }
21. public RadioDemo() {
22. addWindowListener(new WindowAdapter() {
23. public void windowClosing(WindowEvent e) {
24. System.exit(0);
25. }
26. } );
27. setLocation(250,150);
28. Container pane = getContentPane();
29. pane.setLayout(new BoxLayout(pane,BoxLayout.Y_AXIS));
30.
31. ButtonGroup group = new ButtonGroup();//单选按钮分组以保证单一选择
32.
33. label = new JLabel("Ajava.org");
34. pane.add(label);
35.
36. buttonA = new JRadioButton("Aardvark");
37. buttonA.setSelected(true);
38. buttonA.addActionListener(this);
39. group.add(buttonA);
40. pane.add(buttonA);
41.
42. buttonB = new JRadioButton("Baracuda");
43. buttonB.setSelected(false);
44. buttonB.addActionListener(this);
45. group.add(buttonB);
46. pane.add(buttonB);
47.
48. buttonC = new JRadioButton("Condor");
49. buttonC.setSelected(false);
50. buttonC.addActionListener(this);
51. group.add(buttonC);
52. pane.add(buttonC);
53.
54. buttonD = new JRadioButton("Daryll");
55. buttonD.setSelected(false);
56. buttonD.addActionListener(this);
57. group.add(buttonD);
58. pane.add(buttonD);
59.
60. pack();
61. setVisible(true);
62. }
63. public void actionPerformed(ActionEvent e) {
64. String text = "";
65. if(buttonA.isSelected())
66. text = "A";
67. else if(buttonB.isSelected())
68. text = "B";
69. else if(buttonC.isSelected())
70. text = "C";
71. else if(buttonD.isSelected())
72. text = "D";
73. label.setText(text);
74. Object obj = e.getSource();
75. if(obj == buttonA)
76. System.out.println("buttonA");
77. if(obj == buttonB)
78. System.out.println("buttonB");
79. if(obj == buttonC)
80. System.out.println("buttonC");
81. if(obj == buttonD)
82. System.out.println("buttonD");
83. }
84. }