package Demo1;
/**
 * 
 * @author Administrator
 *
 */
import java.awt.*;import javax.swing.*;
import java.awt.event.*;
  public class Test3 extends JFrame  implements ActionListener
{
   public static void main(String[] args)
   {
   Test3 t=new Test3();
   }
   MyPanel1 mp=new MyPanel1();
   JButton jb1=null;
   JButton jb2=null;
   public Test3()
   {
   mp=new MyPanel1();
   Cat cat=new Cat();
   mp.setBackground(Color.black);
   jb1=new JButton("黑色");
   jb2=new JButton("红色");
   
   this.add(jb1,BorderLayout.NORTH);
   this.add(mp);
   this.add(jb2,BorderLayout.SOUTH);
   
   jb1.addActionListener(this);
   jb1.setActionCommand("a");
   jb1.addActionListener(cat);
   
   jb2.addActionListener(this);
   jb2.addActionListener(cat);
   jb2.setActionCommand("b");
   
   this.setSize(300,200);
   this.setVisible(true);
   this.setDefaultCloseOperation
   (JFrame.EXIT_ON_CLOSE);
   }public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("a"))
{
System.out.println("你点了黑丝按钮");
    mp.setBackground(Color.black);
}else if(e.getActionCommand().equals("b"))
{
System.out.println("你点了红色");
mp.setBackground(Color.red);
}
}
}
class MyPanel1  extends JPanel
{
    public void paint(Graphics g)
    {
     super.paint(g);
    }
}
class Cat implements ActionListener
{
public void actionPerformed(ActionEvent d) {
if(d.getActionCommand().equals("a"))
{
System.out.println("猫也知道你点了黑色");

}else if(d.getActionCommand().equals("b"));
{
System.out.println("猫也知道你点了红色");
}
}
  
}我点了黑色 怎么会出现这样结果:
“猫也知道你点了黑色
猫也知道你点了红色
你点了黑丝按钮”

解决方案 »

  1.   

    注意缩进。。
    import java.awt.*;import javax.swing.*;
    import java.awt.event.*;
    public class Test extends JFrame implements ActionListener
    {
      public static void main(String[] args)
      {
    Test t=new Test();
      }
      
      MyPanel1 mp=new MyPanel1();
      JButton jb1=null;
      JButton jb2=null;
      public Test()
      {
    mp=new MyPanel1();
    Cat cat=new Cat();
    mp.setBackground(Color.black);
    jb1=new JButton("黑色");
    jb2=new JButton("红色");
      
    this.add(jb1,BorderLayout.NORTH);
    this.add(mp);
    this.add(jb2,BorderLayout.SOUTH);
      
    jb1.addActionListener(this);
    jb1.setActionCommand("a");
    jb1.addActionListener(cat);
      
    jb2.addActionListener(this);
    jb2.addActionListener(cat);
    jb2.setActionCommand("b");
      
    this.setSize(300,200);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    } public void actionPerformed(ActionEvent e) {
    if(e.getActionCommand().equals("a"))
    {
    System.out.println("你点了黑丝按钮");
    mp.setBackground(Color.black);
    }
    else if(e.getActionCommand().equals("b"))
    {
    System.out.println("你点了红色");
    mp.setBackground(Color.red);
    }
    }
    }
    class MyPanel1 extends JPanel
    {
      public void paint(Graphics g)
      {
      super.paint(g);
      }
    }

    class Cat implements ActionListener
    {
    public void actionPerformed(ActionEvent d) {
    if(d.getActionCommand().equals("a"))
    {
    System.out.println("猫也知道你点了黑色");
    }
    else if(d.getActionCommand().equals("b"))
    //}else if(d.getActionCommand().equals("b"));
    {
    System.out.println("猫也知道你点了红色");
    }
    }
    }
      

  2.   

    }else if(d.getActionCommand().equals("b"));
    {
    System.out.println("猫也知道你点了红色");的 else if(d.getActionCommand().equals("b")); 多了个分号。另外,ActionListener的调用顺序是从上到下,即先按钮后JFrame