import java.awt.*;
import java.awt.event.*;
class MyWindow extends Frame implements ActionListener
{ TextField text1,text2,text3;
  MyWindow()
   {  setLayout(new FlowLayout());
      text1=new TextField(8);
      text2=new TextField(8);
      text3=new TextField(15);
      add(text1);
      add(text2);
      add(text3);
      text1.addActionListener(this);//这里两个this是指win吗?为什么不能写win?不用this还可以怎么写? 
      text2.addActionListener(this);
      setBounds(100,100,150,150);
      setVisible(true);
      validate();
   }  
   public void actionPerformed(ActionEvent e) 
   {  if(e.getSource()==text1) 
      {  String word=text1.getText();
         if(word.equals("boy")) 
           { text3.setText("男孩");
           }
         else if (word.equals("girl")) 
           { text3.setText("女孩");
           }
         else if (word.equals("sun")) 
          {  text3.setText("太阳");
          }
        else 
          {  text3.setText("没有该单词");
          }
      }
    else if(e.getSource()==text2) 
      {  String word=text2.getText();
         if(word.equals("男孩")) 
         {  text3.setText("boy");
         }
         else if (word.equals("女孩"))
          { text3.setText("girl");
          }
        else if (word.equals("太阳")) 
         {  text3.setText("sun");
         }
       else
        {   text3.setText("没有该单词");
        }
      }
   }
}
public class Example7_3
{   public static void main(String args[])
    {  MyWindow win=new MyWindow();
    }
}

解决方案 »

  1.   

    this用在类中,可以指代任何一个该类的实例对象的引用。在你的代码中,由于MyWindow只有一个实例win,所以,this指的就是win。如果,我再创建一个MyWindow win1 = new MyWindow(),这时this指的就是win1。这样解释也不是很准确。总之,你可以想象,win和win1是两个对象,但是共用同一段代码。jvm执行这段代码的时候如何区分当前对象是win还是win1呢,编译器会把正在执行这段代码的那个对象的引用当作参数传入方法中,这个传进来的引用就是this。所以,可以把this理解为一个参数,它是当前类的某一个实例,但你不能确定它具体是哪一个,准确地说,是当前正在执行这段代码的那一个。这些知识是类乃至整个OO机制中最核心的部分,是从过程编程跨入OO大门的第一步。
      

  2.   

    还可以写匿名类..text1.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent ae){
         
      }
    });
      

  3.   

    this指的是你當前程序正在運行中的對象,
    就是你運行到這一步時候的對象就是this,就是說你運行到這步,程序中的對象是什么,this就是什么
    這里也就是win
      

  4.   

    其实当你创建一个对象引用时,同时就有一个THIS指向这个对象,也可以理解为创建了两个引用,只不过THIS是隐含的
      

  5.   

    addActionListener这个方法的形参是实现了ActionListener接口类的对象,而MyWindow类在声明时通过implements ActionListener这一句指明了本类(即MyWindow类)实现了ActionListener接口,所以凡是MyWindow类的对象都可以作为addActionListener方法的实例参数.而this可以理解为指向对象本身(MyWindow类对象)的引用,所以可以作为addActionListener的实例参数.如果你要分开写,也可以单独声明一个实现ActionListener接口的类,比如class MyActionListener implements ActionListener
        {
           public void actionPerformed(ActionEvent e) 
       {  if(e.getSource()==text1) 
          {  String word=text1.getText();
             if(word.equals("boy")) 
               { text3.setText("男孩");
               }
             else if (word.equals("girl")) 
               { text3.setText("女孩");
               }
             else if (word.equals("sun")) 
              {  text3.setText("太阳");
              }
            else 
              {  text3.setText("没有该单词");
              }
          }
        else if(e.getSource()==text2) 
          {  String word=text2.getText();
             if(word.equals("男孩")) 
             {  text3.setText("boy");
             }
             else if (word.equals("女孩"))
              { text3.setText("girl");
              }
            else if (word.equals("太阳")) 
             {  text3.setText("sun");
             }
           else
            {   text3.setText("没有该单词");
            }
          }
       }
    }
    然后在添加事件处理对象时,可以这样写MyActionListener xxx=new MyActionListener;
          text1.addActionListener(xxx);
          text2.addActionListener(xxx);
    效果是一样的.当然也可以采用基于实现匿名内部类的方法,直接在addActionListener方法的实例参数中给出事件处理接口的实现.
    本例可以写为      text1.addActionListener(new ActionListener(){
         public void actionPerformed(ActionEvent e) 
       {  if(e.getSource()==text1) 
          {  String word=text1.getText();
             if(word.equals("boy")) 
               { text3.setText("男孩");
               }
             else if (word.equals("girl")) 
               { text3.setText("女孩");
               }
             else if (word.equals("sun")) 
              {  text3.setText("太阳");
              }
            else 
              {  text3.setText("没有该单词");
              }
          }
        else if(e.getSource()==text2) 
          {  String word=text2.getText();
             if(word.equals("男孩")) 
             {  text3.setText("boy");
             }
             else if (word.equals("女孩"))
              { text3.setText("girl");
              }
            else if (word.equals("太阳")) 
             {  text3.setText("sun");
             }
           else
            {   text3.setText("没有该单词");
            }
          }
       }
    }
         );
    但是这种方法仅限于该事件处理只添加于单个空间中,如果多个控件使用同一个事件处理对象时,使用这个方法就就不太适合,而且这个方法我在学的时候经常会造成几个括号多了或者漏了的错误.班门弄斧一下,希望各位大侠批评指正.
      

  6.   


    请问九楼,一下这句有和原因
    addActionListener这个方法的形参是实现了ActionListener接口类的对象???