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 CheckBoxPanel extends JPanel implements ActionListener { public CheckBoxPanel() { redButton = new JCheckBox("Red", false) ; add(redButton) ; redButton.addActionListener(this) ; } public void actionPerformed(ActionEvent evt) { if(redButton.isSelected()) setBackground(Color.red) ; else setBackground(defaultColor) ; repaint() ; } private JCheckBox redButton ; private Color defaultColor = getBackground() ; } public class CheckBoxTest { public static void main(String [] args) { JFrame frame = new JFrame() ; frame.setTitle("CheckBoxTest") ; frame.setSize(450, 300) ; frame.addWindowListener(new TerminationListener()) ; frame.getContentPane().add(new CheckBoxPanel()) ; frame.setVisible(true) ; } }