import java.awt.* ; import java.awt.event.* ; import javax.swing.* ; class TerminationListener implements WindowListener { public void windowActivated(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowClosing(WindowEvent e) { System.exit(0) ; } public void windowDeactivated(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowOpened(WindowEvent e) {} } class RadioButtonPanel extends JPanel implements ActionListener { public RadioButtonPanel() { redButton = new JRadioButton("Red", true) ; blueButton = new JRadioButton("Blue", false) ; greenButton = new JRadioButton("Green", false) ; add(redButton) ; add(blueButton) ; add(greenButton) ; redButton.addActionListener(this) ; blueButton.addActionListener(this) ; greenButton.addActionListener(this) ; ButtonGroup group = new ButtonGroup() ; group.add(redButton) ; group.add(blueButton) ; group.add(greenButton) ; setBackground(Color.red) ; } public void actionPerformed(ActionEvent evt) { if(redButton.isSelected()) setBackground(Color.red) ; else if(blueButton.isSelected()) setBackground(Color.blue) ; else if(greenButton.isSelected()) setBackground(Color.green) ; repaint() ; } private JRadioButton redButton ; private JRadioButton blueButton ; private JRadioButton greenButton ; } public class RadioButtonTest { public static void main(String [] args) { JFrame frame = new JFrame() ; frame.setTitle("RadioButtonTest") ; frame.setSize(450, 300) ; frame.addWindowListener(new TerminationListener()) ; frame.getContentPane().add(new RadioButtonPanel()) ; frame.setVisible(true) ; } }