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.setVisible(true);
  } 
  
}class ButtonFrame extends JFrame{
  
  private JPanel buttonPanel;
  private JButton yellowButton;
  private JButton redButton;
  private JButton blueButton;
  
  public ButtonFrame(){
    setTitle("ButtonTest");
    setSize(300,200);
    yellowButton = new JButton("Yellow");
    redButton = new JButton("Red");
    blueButton = new JButton("Blue");
    buttonPanel = new JPanel();
    
    buttonPanel.add(yellowButton);
    buttonPanel.add(blueButton);
    buttonPanel.add(redButton);
    
    add(buttonPanel);
   
    yellowButton.addActionListener(this);
    redButton.addActionListener(this);
    blueButton.addActionListener(this);
   } 
    public void actionPerformed(ActionEvent e){
      Object eventSource = e.getSource();
      if(eventSource == yellowButton){
        buttonPanel.setBackground(Color.yellow);
        }else if(eventSource == redButton){
          buttonPanel.setBackground(Color.red);
       }else if(eventSource == blueButton){
        buttonPanel.setBackground(Color.blue);
      }
    }
   } 
    

解决方案 »

  1.   

    没有加入监听器接口加implementsimport 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.setVisible(true); 
      } 
      
    } class ButtonFrame extends JFrame{ 
      
      private JPanel buttonPanel; 
      private JButton yellowButton; 
      private JButton redButton; 
      private JButton blueButton; 
      
      public ButtonFrame(){ 
        setTitle("ButtonTest"); 
        setSize(300,200); 
        yellowButton = new JButton("Yellow"); 
        redButton = new JButton("Red"); 
        blueButton = new JButton("Blue"); 
        buttonPanel = new JPanel(); 
        
        buttonPanel.add(yellowButton); 
        buttonPanel.add(blueButton); 
        buttonPanel.add(redButton); 
        
        add(buttonPanel); 
      
        yellowButton.addActionListener(this); 
        redButton.addActionListener(this); 
        blueButton.addActionListener(this); 
      } 
        public void actionPerformed(ActionEvent e){ 
          Object eventSource = e.getSource(); 
          if(eventSource == yellowButton){ 
            buttonPanel.setBackground(Color.yellow); 
            }else if(eventSource == redButton){ 
              buttonPanel.setBackground(Color.red); 
          }else if(eventSource == blueButton){ 
            buttonPanel.setBackground(Color.blue); 
          } 
        } 
      } 
        
      

  2.   

    上面的粘贴到你错误的
    不好意思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.setVisible(true); 
      } 
      
    } class ButtonFrame extends JFrame implements ActionListener{ 
      
      private JPanel buttonPanel; 
      private JButton yellowButton; 
      private JButton redButton; 
      private JButton blueButton; 
      
      public ButtonFrame(){ 
        setTitle("ButtonTest"); 
        setSize(300,200); 
        yellowButton = new JButton("Yellow"); 
        redButton = new JButton("Red"); 
        blueButton = new JButton("Blue"); 
        buttonPanel = new JPanel(); 
        
        buttonPanel.add(yellowButton); 
        buttonPanel.add(blueButton); 
        buttonPanel.add(redButton); 
        
        add(buttonPanel); 
      
        yellowButton.addActionListener(this); 
        redButton.addActionListener(this); 
        blueButton.addActionListener(this); 
      } 
        public void actionPerformed(ActionEvent e){ 
          Object eventSource = e.getSource(); 
          if(eventSource == yellowButton){ 
            buttonPanel.setBackground(Color.yellow); 
            }else if(eventSource == redButton){ 
              buttonPanel.setBackground(Color.red); 
          }else if(eventSource == blueButton){ 
            buttonPanel.setBackground(Color.blue); 
          } 
        } 
      } 
        
      

  3.   

    yellowButton.addActionListener(this); 
    redButton.addActionListener(this); 
    blueButton.addActionListener(this); 没有声明监听接口
      

  4.   

    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.setVisible(true); 
      } 
      
    } class ButtonFrame extends JFrame implements ActionListener
      
      private JPanel buttonPanel; 
      private JButton yellowButton; 
      private JButton redButton; 
      private JButton blueButton; 
      
      public ButtonFrame(){ 
        setTitle("ButtonTest"); 
        setSize(300,200); 
        yellowButton = new JButton("Yellow"); 
        redButton = new JButton("Red"); 
        blueButton = new JButton("Blue"); 
        buttonPanel = new JPanel(); 
        
        buttonPanel.add(yellowButton); 
        buttonPanel.add(blueButton); 
        buttonPanel.add(redButton); 
        
        add(buttonPanel); 
      
        yellowButton.addActionListener(this); 
        redButton.addActionListener(this); 
        blueButton.addActionListener(this); 
      } 
        public void actionPerformed(ActionEvent e){ 
          Object eventSource = e.getSource(); 
          if(eventSource == yellowButton){ 
            buttonPanel.setBackground(Color.yellow); 
            }else if(eventSource == redButton){ 
              buttonPanel.setBackground(Color.red); 
          }else if(eventSource == blueButton){ 
            buttonPanel.setBackground(Color.blue); 
          } 
        } 
      } 
        
      

  5.   


    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.setVisible(true); 
      } 
      
    } class ButtonFrame extends JFrame{ 
      
      private JPanel buttonPanel; 
      private JButton yellowButton; 
      private JButton redButton; 
      private JButton blueButton; 
      
      public ButtonFrame(){ 
        setTitle("ButtonTest"); 
        setSize(300,200); 
        yellowButton = new JButton("Yellow"); 
        redButton = new JButton("Red"); 
        blueButton = new JButton("Blue"); 
        buttonPanel = new JPanel(); 
        
        buttonPanel.add(yellowButton); 
        buttonPanel.add(blueButton); 
        buttonPanel.add(redButton); 
        
        add(buttonPanel); 
      
        yellowButton.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent ae){
    buttonPanel.setBackground(Color.yellow); 
    }
    });
        
        redButton.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent ae){
     buttonPanel.setBackground(Color.red); 
    }
    });
        blueButton.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent ae){
     buttonPanel.setBackground(Color.blue); 
    }
    });
      } 
        
      } 
        
      

  6.   

    class ButtonFrame extends JFrame implements ActionListener
    实现ActionListener