Java“.addActionListener(this)”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3124126/
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
Java ".addActionListener(this)"
提问by
If I add a Action Listener then I always use the "this" between the brackets. But what does this "this" stand for ?!
如果我添加一个动作侦听器,那么我总是在括号之间使用“this”。但是这个“这个”是什么意思?!
回答by James T
The addActionListener method takes the current class object as a parameter. The "this" key word simply means "this object I'm working in right now". If you are using netbeans, you can type "this" and put a period after it to see all the methods defined in "this" class. It should list all the methods that are defined in your class including any inherited methods.
addActionListener 方法将当前类对象作为参数。“this”关键字仅表示“我现在正在处理的这个对象”。如果您使用的是 netbeans,您可以键入“this”并在其后放置一个句点以查看“this”类中定义的所有方法。它应该列出您的类中定义的所有方法,包括任何继承的方法。
In order to fully understand what "this" means, you must first understand the relationship between classes and objects.
为了充分理解“this”是什么意思,首先要理解类和对象的关系。
If you want to be technical about it, "this" is a reference to the current object.
如果你想从技术上讲,“this”是对当前对象的引用。
回答by Romain Hippeau
If you look at the tutorialthen you will see...
如果你看教程,那么你会看到......
To write an Action Listener, follow the steps given below:
Declare an event handler class and specify that the class either implements an ActionListener interface or extends a class that implements an ActionListener interface.
For example:
要编写 Action Listener,请按照以下步骤操作:
声明一个事件处理程序类并指定该类要么实现 ActionListener 接口,要么扩展实现 ActionListener 接口的类。
例如:
public class MyClass implements ActionListener {
Register an instance of the event handler class as a listener on one or more components.
For example:
将事件处理程序类的实例注册为一个或多个组件上的侦听器。
例如:
someComponent.addActionListener(instanceOfMyClass);
Include code that implements the methods in listener interface.
For example:
在侦听器接口中包含实现方法的代码。
例如:
public void actionPerformed(ActionEvent e) {
...//code that reacts to the action...
}
The this represents an implemented and instantiated ActionListener, which happens to be your class. You could very well pass any class that implements the ActionListener interface.
this 表示一个已实现和实例化的 ActionListener,它恰好是您的类。您可以很好地传递任何实现 ActionListener 接口的类。
That way when a button is pressed the your actionPerformed method will be called
这样,当按下按钮时,您的 actionPerformed 方法将被调用
回答by mikera
"this" stands for the current class instance that you are inside.
“this”代表您所在的当前类实例。
It will work as long as the class implements the ActionListener interface.
只要该类实现 ActionListener 接口,它就会工作。
You can use a completely different object if you like, e.g. creating an anonymous inner class that implements the ActionListener interface:
如果愿意,您可以使用完全不同的对象,例如创建一个实现 ActionListener 接口的匿名内部类:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Hello!!!");
}
});
This is helpful if you want to have multiple different action listeners but don't want to create separate classes for each.
如果您想拥有多个不同的动作侦听器,但又不想为每个侦听器创建单独的类,这将很有帮助。