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 ButtonPanel extends JPanel implements ActionListener { public ButtonPanel() { redButton = new JButton("Red") ; blueButton = new JButton("Blue") ; greenButton = new JButton("Green") ; add(redButton) ; add(blueButton) ; add(greenButton) ; redButton.addActionListener(this) ; blueButton.addActionListener(this) ; greenButton.addActionListener(this) ; } public void actionPerformed(ActionEvent evt) { if(evt.getSource() == redButton) setBackground(Color.red) ; else if(evt.getSource() == blueButton) setBackground(Color.blue) ; else if(evt.getSource() == greenButton) setBackground(Color.green) ; repaint() ; } private JButton redButton ; private JButton blueButton ; private JButton greenButton ; } public class ButtonTest { public static void main(String [] args) { JFrame frame = new JFrame() ; frame.setTitle("ButtonTest") ; frame.setSize(450, 300) ; frame.addWindowListener(new TerminationListener()) ; frame.getContentPane().add(new ButtonPanel()) ; frame.setVisible(true) ; } }