你的这种方法不好,这样做吧
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class button extends JFrame
{
public button()
{
JButton jb1=new JButton(" 1 ");
JButton jb2=new JButton(" 2 ");
getContentPane().setLayout(new BorderLayout());
getContentPane().add(jb1,BorderLayout.NORTH);
getContentPane().add(jb2,BorderLayout.SOUTH);
jb1.addActionListener(new action_jb1());
jb2.addActionListener(new action_jb2());
setSize(300,300);
show();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String args[])
{
new button();
}
class action_jb1 implements ActionListener
{
public void actionPerformed(ActionEvent actionevent)
        {
//do something
System.out.println("this is 1");
}
action_jb1(){}
}
class action_jb2 implements ActionListener
{
public void actionPerformed(ActionEvent actionevent)
        {
//do something
System.out.println("this is 2");
}
action_jb2(){}
}
}如果你的两个button是事件是一样的,那么这两句
jb1.addActionListener(new action_jb1());
jb2.addActionListener(new action_jb2());
就改为:
jb1.addActionListener(new action_jb1());
jb2.addActionListener(new action_jb1());
也就是用同一个动作

解决方案 »

  1.   

    e.getSource()就是得到事件源。。可以通过定义一个Object
    来检测按下了哪个按钮Object eveSrc = e.getSource();
            if(eveSrc instanceof Button)
            {
                if(eveSrc == btn1)
      

  2.   

    class Closer extends WindowAdapter
    {   public void windowClosing (WindowEvent e)
        {   System.exit(0);
        }
    }class TestFrame extends Frame implements ActionListener
    {   String Close="Close";
        String Ok="Ok";    public TestFrame ()
        {   super("Test Frame");
            setSize(300,300);
            
            Panel P=new Panel();         
     
            Button A=new Button(Ok);
            A.addActionListener(this); 
            P.add(A);         
            add(P);         
            setVisible(true);
            Button B=new Button(Close);
            B.addActionListener(this); 
            P.add(B);         
            add(P);         
            setVisible(true);
            
            addWindowListener(new Closer());
        }
        
        public void actionPerformed (ActionEvent e)
        {   if (e.getActionCommand().equals(Close))
            {   dispose(); System.exit(0);         }
            if (e.getActionCommand().equals(Ok))
            {   
                    //add code here!!
            }
              
        }
    }你可以用getActionCommand来判断点了哪个button.
    e.getSource() instanceof Button 的意思是判断event的发生源是否是一个button.
      

  3.   

    来晚了。up
    up
    and up
      

  4.   

    void jButton1_actionPerformed(ActionEvent e) {
          }
    ///////////////////////////////////////////////////////////
      void jButton3_actionPerformed(ActionEvent e) {
          }
    分开比较好