这段代码有问题,我找不出来,麻烦各位高手帮帮忙class ButtonPanel extends JPanel{
//create buttons 
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");
//add buttons to panel 
add(yellowButton);
add(blueButton);
add(redButton);
    
//create button action
ColorAction yellowAction = new ColorAction(Color.YELLOW);
ColorAction buleAction = new ColorAction(Color.BLUE);
ColorAction redAction = new ColorAction(Color.RED);
//associate actions with buttons
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(buleAction);
redAction.addActionListener(redAction);
//An action listener that sets implements ActionListener
private class ColorAction implements ActionListener{
private Color backgroundColor;
public ColorAction(Color c){
backgroundColor = c;
}
public void actionPerformed(ActionEvent event){
setBackground(backgroundColor);

}
}
}
class前面没有public是ButtonPanel因为是内部类

解决方案 »

  1.   

    class ButtonPanel extends JPanel {
    JButton yellowButton = new JButton("Yellow");
    JButton blueButton = new JButton("Blue");
    JButton redButton = new JButton("Red");
    ColorAction yellowAction = new ColorAction(Color.YELLOW);
    ColorAction buleAction = new ColorAction(Color.BLUE);
    ColorAction redAction = new ColorAction(Color.RED); public ButtonPanel() {
    add(yellowButton);
    add(blueButton);
    add(redButton);
    yellowButton.addActionListener(yellowAction);
    blueButton.addActionListener(buleAction);
    redButton.addActionListener(redAction);
    } private class ColorAction implements ActionListener {
    private Color backgroundColor; public ColorAction(Color c) {
    backgroundColor = c;
    } public void actionPerformed(ActionEvent e) {
    setBackground(backgroundColor);
    }
    }
    }
      

  2.   

    你能不能说一下运行的机制,JPanel 类是没有 public void add(Component)方法的