代码修改如下:
自己分析原因import javax.swing.*;
import java.awt.*;
import java.awt.event.*;class javaButton
{
        public static void main(String[] args)
        {
                ButtonTestFrame frame = new ButtonTestFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.show();
        }
}class ButtonTestFrame extends JFrame
{
        public ButtonTestFrame()
        {
                setTitle("Button");
                setSize(200, 400);                 ButtonPanel panel = new ButtonPanel();
                 Container contentPane = getContentPane();
                 contentPane.add(panel);
         }
}class ButtonPanel extends JPanel
{
        public ButtonPanel()
        {
                JButton yellowButton = new JButton("Yellow");
                add(yellowButton);
                ColorAction yellowAction = new ColorAction(Color.yellow,this);
                yellowButton.addActionListener(yellowAction);
        }
}class ColorAction implements ActionListener
{
        public ColorAction(Color back,JPanel backpane)
        {
                backgroundColor = back;
                bpane = (ButtonPanel)backpane;
        }        public void actionPerformed(ActionEvent event)
        {                bpane.setBackground(backgroundColor);
                bpane.repaint();
        }        private Color backgroundColor;
        private ButtonPanel bpane;
}