java addActionListener(this) 和 addActionListener(new ActionListener) 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7435622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between addActionListener(this) and addActionListener(new ActionListener)?
提问by alan
I want to add event handling to buttons- and I noticed that there are two ways to do this.
我想为按钮添加事件处理 - 我注意到有两种方法可以做到这一点。
- Implement the ActionListener interface and then append event listeners to the buttons.
- 实现 ActionListener 接口,然后将事件侦听器附加到按钮。
Example:
例子:
countButton.addActionListener(this);
And the in the ActionPerformed method will run to display the result.
并且 ActionPerformed 方法中的 将运行以显示结果。
Do not implement the ActionListener interface and instead do this:
countButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //Execute when button is pressed System.out.println("You clicked the button"); } });
不要实现 ActionListener 接口,而是这样做:
countButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //Execute when button is pressed System.out.println("You clicked the button"); } });
How does the second method work exactly?????!!!
第二种方法究竟是如何工作的???????!!!
Thanks!
谢谢!
回答by Costis Aivalis
It is not necessary to define a second class for the first approach. You just need to add the public void actionPerformed(ActionEvent e)
method inside your class and do whatever you want there, after you make your class implement ActionListener. You could use a second class if you wanted to, but it is not necessary. The disadvantage is that you need to specify the source of the event with long if statements in order to take the appropriate action if you have more than one JButtons i.e.
没有必要为第一种方法定义第二个类。你只需要public void actionPerformed(ActionEvent e)
在你的类中添加方法并在你让你的类实现 ActionListener 之后做任何你想做的事情。如果您愿意,您可以使用第二个类,但这不是必需的。缺点是你需要用长 if 语句指定事件的来源,以便在你有多个 JButton 时采取适当的行动,即
The second approach is adding an anonymous inner ActionListener to every component. It is a more Object Oriented approach, since you have a clearer separation of the functionality of your widgets. It is of advantage to define an additional method called inside each actionPerformed, in order to be able to use thisfreely and refer to the containing class:
第二种方法是向每个组件添加一个匿名内部 ActionListener。这是一种更加面向对象的方法,因为您可以更清晰地分离小部件的功能。定义一个在每个 actionPerformed 内部调用的附加方法是有好处的,以便能够自由地使用它并引用包含类:
countButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
doCountButtonAction(e);
// "this" here would refer to the ActionListener (not that useful)
}
});
and implement the method:
并实现该方法:
private void doCountButtonAction(ActionEvent e) {
// do whatever you need to here
// using "this" here refers to the containing class (i.e. JFrame, JPanel or whatever)
}
回答by Shivan Dragon
Your question is not exactly on listeners as it is on how interfaces work and how you can instantiate a class in Java. Here's some finer points:
您的问题不完全是关于侦听器,而是关于接口如何工作以及如何在 Java 中实例化一个类。这里有一些更好的观点:
- Basicaly, what the JButton class offers you is a way to declare a class who's one particular method will be called when an event is triggered on the button, like when it is clicked. If you look at the Javadocs for JButton and ActionListener, you will now how they work:
- 基本上,JButton 类为您提供了一种声明一个类的方法,该类的一个特定方法将在按钮上触发事件时调用,例如单击按钮时。如果您查看 JButton 和 ActionListener 的 Javadoc,您将了解它们的工作原理:
http://download.oracle.com/javase/1.4.2/docs/api/java/awt/event/ActionListener.html
http://download.oracle.com/javase/1.4.2/docs/api/java/awt/event/ActionListener.html
What you can do here in the most old fashioned way possible is to create a class that will be triggered when someone clicks your button:
您可以在这里以最老式的方式做的是创建一个类,当有人单击您的按钮时将触发该类:
public class MyButtonActionListener implements ActionListener {
actionPerformed(ActionEvent e) {
System.out.println("Aww, you clicked!");
}
}
Now, once that's done, you can make an insance of it and add it as a listener to your button:
现在,一旦完成,您可以对其进行分析并将其添加为按钮的侦听器:
JButton button = new JButton("My button");
MyButtonActionListener myActionListener = new MyButtonActionListener (); button.addActionListener(myActionListener);
MyButtonActionListener myActionListener = new MyButtonActionListener(); button.addActionListener(myActionListener);
On the other hand, in Java you can instantiate a class anonimousy, which means that instead of having a handler to it's instance (like myActionListener in the above code), you just instantiate it on the fly in the place you need it, and you'll have no handler to use it later. That's what's happening in your code: an ActionListener implementation is delcared on the fly as the parameter for the addActionListener method, that on the fly declaration also includes the statement that your anonymous instance is not just any class, but one that implements ActionListener, and such your anonymous declaration needs to give an implementation of the actionPerformed method.
另一方面,在 Java 中,您可以实例化一个类 anonimousy,这意味着无需为其实例设置处理程序(如上面代码中的 myActionListener),您只需在需要的地方即时实例化它,然后' 以后将没有处理程序可以使用它。这就是您的代码中发生的事情:ActionListener 实现被动态声明为 addActionListener 方法的参数,动态声明还包括声明您的匿名实例不仅仅是任何类,而是实现 ActionListener 的类,等等您的匿名声明需要提供 actionPerformed 方法的实现。
A third option is to have a class that implements ActionListener (and the actionPerformed method), and inside that class, if you create a JButton and you want to pass it as listener an instance of the ecompasing class, you'll use "this" to reffer to that, as in :
第三种选择是拥有一个实现 ActionListener(和 actionPerformed 方法)的类,并且在该类中,如果您创建一个 JButton 并且您想将它作为侦听器传递给 ecompassing 类的一个实例,您将使用“this”指的是,如:
public class MyButtonActionListener implements ActionListener {
private JButton button = new JButton();
public void init() {
button.addActionListener(this);
}
public actionPerformed(ActionEvent e) {
System.out.println("u clicked!");
}
}
There's alot of finer points to this discussion (as in how do you reffer to the "this" on a n anonymous class delcared inside an other class, and how do you reffer to the "this" of the encompassing class instance). I recommend that you read a book on the Sun Certified Java Programmer certification, it has a chapter dedicated to this
这个讨论有很多更好的观点(比如你如何引用另一个类中声明的匿名类上的“this”,以及你如何引用包含类实例的“this”)。我建议你阅读一本关于 Sun Certified Java Programmer 认证的书,里面有一章专门介绍这个
回答by mamboking
That just creates an anonymous class that implements the actionPerformed method. This way you don't have to define a new class for every place that you want to handle an event.
这只是创建了一个实现 actionPerformed 方法的匿名类。这样您就不必为要处理事件的每个地方都定义一个新类。
回答by Usman Saleem
It works the same way as the first technique i.e. The method expects an object which implement ActionListener interface. However, both techniques has its own merits and demerits. The first technique allows utilizing single object on the expense of cluttered if/else code because same method will be handling events for multiple buttons. The second technique allows cleaner separation of logic for each button on the expense of creating multiple anonymous objects (for each button).
它的工作方式与第一种技术相同,即该方法需要一个实现 ActionListener 接口的对象。然而,这两种技术都有其自身的优点和缺点。第一种技术允许以混乱的 if/else 代码为代价使用单个对象,因为相同的方法将处理多个按钮的事件。第二种技术允许更清晰地分离每个按钮的逻辑,但代价是创建多个匿名对象(为每个按钮)。
回答by BenCole
Either way, to add an ActionListener you need a class that implements the ActionListener interface (which defines a single method, actionPerformed
). addActionListener(this)
implies that this
implements that interface, and any action performed will call this.actionPerformed
.
无论哪种方式,要添加 ActionListener,您都需要一个实现 ActionListener 接口(定义单个方法actionPerformed
)的类。 addActionListener(this)
意味着this
实现该接口,并且执行的任何操作都将调用this.actionPerformed
.
In the second case, what you're doing is creating a new, anonymous, class that implements the ActionListener interface. When countButton
is clicked, the actionPerformed
method called is the one in the anonymous class.
在第二种情况下,您所做的是创建一个新的匿名类来实现 ActionListener 接口。当countButton
被点击,actionPerformed
调用的方法是一个在匿名类。