AdderFrame.addWindowListener(new WindowAdapter() {
       public void windowClosing(WindowEvent e)
{
       System.exit(0);
    }
    });put the code into Constructor Add()this means this object
when you click button BAdd, actionPerformed() is called

解决方案 »

  1.   

    --将AdderFrame.addWindowListener方法提前到Adder()中import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class Adder implements ActionListener
    {
    JFrame AdderFrame;
    JTextField TOprand1;
    JTextField TOprand2;
    JLabel LAdd,LSum;
    JButton BAdd;
    int i=0;
    public Adder()
    {
      AdderFrame=new JFrame("AdderFrame");
          TOprand1=new JTextField("0.0");
      TOprand2=new JTextField("0.0");
      LAdd=new JLabel("+");
      LSum=new JLabel("=     ");
      BAdd=new JButton("ADD!");
      AdderFrame.getContentPane().setLayout(new FlowLayout());
      AdderFrame.getContentPane().add(TOprand1);
      AdderFrame.getContentPane().add(TOprand2);
      AdderFrame.getContentPane().add(LAdd);
      AdderFrame.getContentPane().add(TOprand2);
      AdderFrame.getContentPane().add(LSum);
      AdderFrame.getContentPane().add(BAdd);
      AdderFrame.pack();
      AdderFrame.setVisible(true);
              AdderFrame.addWindowListener(new WindowAdapter() //此行提前
    {
    public void windowClosing(WindowEvent e)
          {
         System.exit(0);
      }
    });
      //为命令按钮添加事件监听器,监听器的实参是Adder类的对象
      BAdd.addActionListener(this);  //this 指的是什么
    }
      //Adder类的事件服务方法
      public void actionPerformed(ActionEvent event)  //这个方法什么时候被执行
    {
           i=(i+1)%2;
         if(i==1)
        {
          //i为奇数,求和
        int sum=(int)(Double.valueOf(TOprand1.getText()).doubleValue()+Double.valueOf(TOprand2.getText()).doubleValue());
    LSum.setText("="+sum);
    }
    else
         {
       TOprand1.setText("0.0");
    TOprand2.setText("0.0");
    LSum.setText("= ");
     }
        }
    public static void main(String[] args)
    {
               Adder adder=new Adder();
            }
    }
      

  2.   

    public void actionPerformed(ActionEvent event)  //这个方法什么时候被执行
      当执行BAdd.addActionListener(this)语句时,this指向的就是 actionPerformed方法
      

  3.   

    to: wobelisk() 
      要是我有两个或更多的按扭呢,是不是都called actionPerformed(),要是我想要几个按扭执行不同的方法,是不是就得另换一种方法了
    to : ddbean(丁大伯)我已提前,但以出现错误:
    Adder.java:35: illegal start of expression
              public void actionPerformed(ActionEvent event)
              ^
    1 error
      

  4.   

    backButton.addActionListener
    (
    new ActionListener()
    {
    public void actionPerformed(ActionEvent event)
    {
    URL url=webBrowserPane.back();
    urlTextField.setText(url.toString());
    }
    }
    );
      

  5.   

    上面是一个Button监听事件的例子。
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;public class Adder implements ActionListener
    {
    JFrame AdderFrame;
    JTextField TOprand1;
    JTextField TOprand2;
    JLabel LAdd,LSum;
    JButton BAdd;
    int i=0;
    Graphics g=null;
    public Adder()
    {
      AdderFrame=new JFrame("AdderFrame");
          TOprand1=new JTextField("0.0");
      TOprand2=new JTextField("0.0");
      LAdd=new JLabel("+");
      LSum=new JLabel("=     ");
      BAdd=new JButton("ADD!");
      AdderFrame.getContentPane().setLayout(new FlowLayout());
      AdderFrame.getContentPane().add(TOprand1);
      AdderFrame.getContentPane().add(TOprand2);
      AdderFrame.getContentPane().add(LAdd);
      AdderFrame.getContentPane().add(TOprand2);
      AdderFrame.getContentPane().add(LSum);
      AdderFrame.getContentPane().add(BAdd);
      AdderFrame.pack();
      AdderFrame.setVisible(true);
      AdderFrame.addWindowListener(new WindowAdapter()  {
                     public void windowClosing(WindowEvent e)
           {
                 System.exit(0);
           }
              });
           //为命令按钮添加事件监听器,监听器的实参是Adder类的对象
      BAdd.addActionListener(this);  //this 指的是什么
    }
      //Adder类的事件服务方法
      public void actionPerformed(ActionEvent event)  //这个方法什么时候被执行
    {
           i=(i+1)%2;
         if(i==1)
        {
          //i为奇数,求和
        int sum=(int)(Double.valueOf(TOprand1.getText()).doubleValue()+Double.valueOf(TOprand2.getText()).doubleValue());
    LSum.setText("="+sum);
    }
    else
         {
       TOprand1.setText("0.0");
    TOprand2.setText("0.0");
    LSum.setText("= ");
     }
        }public static void main(String[] args)
    {
               Adder adder=new Adder();
            }
    }
      

  6.   

    //为命令按钮添加事件监听器,监听器的实参是Adder类的对象
      BAdd.addActionListener(this);  //this 指的是什么
    }this 实际上表示一个ActionEvent, Adder类implement ActinoLisenter接口所以在Adder中要实现actionPerformed()方法,当button出触发ActionEvent时调用,actionPerformed()方法
    若是有两个以上的事件源,可以用ActionEven的getSource()方法来获得事件发生的"地点“
      

  7.   

    Adder.java:35: illegal start of expression
              public void actionPerformed(ActionEvent event)
              ^
    1 error
    那么为什么会出现上面的错误
      

  8.   

    HELP ME!!!  HELP ME!!!  HELP ME!!!