在完成了我在Java中的第一套课程之后,我现在正在编写Java课程。在本课中,我们将致力于不同组件(按钮、工具栏等)之间的通信。我们一直在学习。问题是,"this“作为addActionListener()方法的方法参数传递。不过,我并不完全明白它在做什么。我做了一些关于"this“的研究,发现"this”关键字最流行的用法是在具有相同名称的变量的构造函数中。我找不到适合我的例子。通过查看下面的代码,我将解释我理解的部分代码。
import java.awt.FlowLayout; Implements FlowLayout class
import java.awt.event.ActionEvent; //Imports ActionEvent Class
import java.awt.event.ActionListener; //Imports ActionListener Interface
import javax.swing.JButton; //Imports JButton class
import javax.swing.JPanel; //Imports JPanel class
public class Toolbar extends JPanel implements ActionListener {
// ^ Inherits JPanel & Implements ActionListener Interface
private JButton helloButton; //Creating variable of JButton type
private JButton goodbyeButton; //Creating variable of JButton type
public Toolbar() { //Constructor
helloButton = new JButton("Hello"); //Creates new JButton
goodbyeButton = new JButton("Goodbye"); //Creates new JButton
helloButton.addActionListener(this); //Question 1
goodbyeButton.addActionListener(this); //Question 1
setLayout(new FlowLayout(FlowLayout.LEFT)); //Sets Layout Type to Left
add(helloButton); //Adds button to FlowLayout (Layout Manager) Interface
add(goodbyeButton); //Adds button to FlowLayout (Layout Manager) Interface
}
public void setTextPanel(TextPanel textPanel) {
//No Usage Yet!
}
public void actionPerformed(ActionEvent arg0) { //Unimplemented Method from ActionListener
System.out.println("A button was clicked"); //Prints after button is clicked.
}
}问题1:如您所见,在创建两个JButtons之后,我们将添加一个addActionListener()方法,以查看是否已单击按钮。如果它已被单击,那么它将执行在actionPerformed中键入的任何代码,这些代码是从ActionListener接口实现的。在以前的课程中,我们有一种不同的方法使按钮响应点击。它做了同样的事情,但看起来是这样的:
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textPanel.appendText("Hello\n");
}
});在本例中,我们只是将文本附加到来自自定义组件的文本区域,而不是将文本打印到控制台以测试actionListener()是否有效。在本文前面的代码中,我们使用的是一个匿名类。
所以..。问题是,在addActionListener()中将其作为方法参数传递时,“此”究竟在做什么?我知道它正在将"A按钮被单击“打印到控制台,但我不明白”这个“是如何将按钮单击到actionPerformed()的背后的逻辑。
这就是应用程序的样子:

这是运行中的应用程序,单击后按钮已经打印到控制台。我想这能让你更好地了解我在做什么。我希望有人能对此有所了解,并解释“这”是如何在这方面起作用的。谢谢!
发布于 2015-06-18 16:09:40
此表示类的当前实例。
工具栏类实现ActionListener接口,这意味着它提供了方法actionPerformed的实现。
因此,helloButton.addActionListener(this);成为可能(尝试从类声明中删除实现ActionListener,代码不会编译)。
这么说,工具栏实例可以被视为ActionListener对象,并可以传递给button.addActionListener。
在使用时
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
}
}您可以动态地创建ActionListener接口的新实现。这种实现称为匿名。
这两个解决方案都是有效的。如果actionPerformed中的代码不能从其他地方使用,则首选匿名解决方案。
发布于 2015-06-18 16:07:07
this只是Toolbar类的一个实例。它在实例方法中用于表示当前实例。实际上,您可以想象您的类有另一个名为this的私有成员变量,它指向一个Toolbar对象。
由于Toolbar实现了ActionListener,所以将新实例化的Toolbar对象作为侦听器分配给按钮(这意味着单击按钮时将调用Toolbar.actionPerformed()方法)。
发布于 2015-06-18 16:04:17
通过实现ActionListener,然后将this传递给addActionListener方法,您将被告知通过调用actionPerformed方法执行的任何操作。
或者,更简单地说,你是给按钮你的电话号码,并要求它打电话给你,每当发生什么事。
在前面的课程中,您正在说明应该发生什么--打印此文本或向textPanel添加一些文本。要做到这一点,你是在做一个ActionListener的飞行。现在,您可以自己实现ActionListener,您可以请求一个回调到,您的,而不是让一个侦听器在运行中。
https://stackoverflow.com/questions/30920199
复制相似问题