next up previous contents
Next: CheckBoxFrame.java Up: Checkbox Previous: Checkbox   Contents

CheckBoxPanel.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class CheckBoxPanel extends JPanel implements ActionListener{

    private JCheckBox redBox;
    private JCheckBox blueBox;

    public CheckBoxPanel(){

        redBox = new JCheckBox("Red");
        blueBox = new JCheckBox("Blue");

        redBox.addActionListener(this);
        blueBox.addActionListener(this);

        redBox.setSelected(false);
        blueBox.setSelected(false);

        add(redBox);
        add(blueBox);
        
    }
        
    public void actionPerformed(ActionEvent evt){

        Color color = getBackground();

        if (blueBox.isSelected()) color = Color.blue;
        if (redBox.isSelected()) color = Color.red;
        if (blueBox.isSelected() && redBox.isSelected()) color = Color.green;

        setBackground(color);
        repaint();
    }
}



Madhavan Mukund 2004-04-29