窗口三个按钮程序的错误,请帮忙解决!(书中原例子)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonTest{
public static void main(String []args)
{
ButtonFrame frame=new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("ButtonTest");
setSize(300,300);
ButtonPanel panel=new ButtonPanel();
Container contentPane=getContentPane();
contentPane.add(panel);
}
}
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
JButton b1=new JButton("Yellow");
JButton b2=new JButton("Blue");
JButton b3=new JButton("Red");
add(b1);
add(b2);
add(b3);
ColorAction yellowAction=new ColorAction(Color.yellow);
ColorAction blueAction=new ColorAction(Color.blue);
ColorAction redAction=new ColorAction(Color.red);
b1.addActionListener(yellowAction);
b2.addActionListener(blueAction);
b3.addActionListener(redAction);
}
}
class ColorAction implements ActionListener
{
private Color backgroundColor;
public ColorAction(Color c)
{
backgroundColor=c;
}
public void actionPerformed(ActionEvent event)
{
setBackground(Color.yellow);//错误出现处~~
}
}