import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonPanel extends JPanel implements ActionListener{
private JButton yellowButton;
private JButton blueButton;
private JButton redButton;
public ButtonPanel(){
yellowButton = new JButton("Yellow");
blueButton = new JButton("Blue");
redButton = new JButton("Red");
yellowButton.setActionCommand("YELLOW");
blueButton.setActionCommand("BLUE");
redButton.setActionCommand("RED");
add(yellowButton);
add(blueButton);
add(redButton);
}
public void actionPerformed(ActionEvent evt){
Color color = getBackground();
String cmd = evt.getActionCommand(); // Use ActionCommand to
// determine what to do
if (cmd.equals("YELLOW")) color = Color.yellow;
else if (cmd.equals("BLUE")) color = Color.blue;
else if (cmd.equals("RED")) color = Color.red;
setBackground(color);
repaint();
}
public void addListener(ActionListener o){
yellowButton.addActionListener(o); // Add a commmon listener
blueButton.addActionListener(o); // for all buttons in
redButton.addActionListener(o); // this panel
}
}