2011-04-09 62 views

回答

2

注册为将一个类的对象引用保存到另一个类。

例如,

JButton button = new JButton("Click Me"); 
    ActionListener listener = new ActionListener() { 
     public void actionPerformed(ActionEvent actionEvent) { 
      System.out.println("YOU CLICKED ME"); 
     } 
    }; 

    button.addActionListener(listener); // register me 

在上面的代码,ActionListener对象的匿名子类注册button对象JButton类的。 button实例反过来会通过调用actionPerformed()它保存的已注册实例的方法来通知单击事件。

+0

嗨,约翰Woderfully解释。 – susan 2011-04-09 04:59:59

+0

你为什么要调用 - “ActionListener对象的匿名子类被注册到类JButton的按钮对象”。作为侦听器是ActionListener类的refid。 – susan 2011-04-09 05:04:33

+0

@susan:ActionListener是一个接口。实现ActionListener的匿名类的引用被传递给addActionListener() – 2011-04-09 05:15:28