1 public class SwingTest123 extends JFrame implements ActionListener { 2 3 JRadioButton boy, girl; 4 JLabel mess; 5 ButtonGroup group; 6 7 public SwingTest123() { 8 init(); 9 setBounds(100, 200, 200, 200);10 setVisible(true);11 }12 13 public void init() {14 setLayout(null);15 Container c = getContentPane();16 17 boy = new JRadioButton("boy");18 boy.setBounds(20, 50, 50, 35);19 girl = new JRadioButton("girl");20 girl.setBounds(100, 50, 50, 35);21 mess = new JLabel("hello");22 mess.setBounds(30, 100, 100, 25);23 24 group = new ButtonGroup();25 group.add(boy);26 group.add(girl);27 c.add(boy);28 c.add(girl);29 c.add(mess);30 boy.addActionListener(this);31 girl.addActionListener(this);32 33 }34 35 String radioText = null;36 37 @Override38 public void actionPerformed(ActionEvent e) {39 if (e.getSource() == boy) {40 radioText = boy.getText();41 42 System.out.println(radioText);43 } else if (e.getSource() == girl) {44 radioText = girl.getText();45 System.out.println(radioText);46 }47 mess.setText("You are a " + radioText);48 49 }50 51 public static void main(String[] args) {52 SwingTest123 st = new SwingTest123();53 54 }55 56 }