Java addActionListener 做什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2167751/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:20:13  来源:igfitidea点击:

What addActionListener does?

javabutton

提问by Roman

I have the following code:

我有以下代码:

JButton button = new JButton("Clear");
button.addActionListener(this);

As far as I understand I create a button on which it is written "Clear". Then I have to associate an action with this button (what will happen if the button is pressed) and it is done by addActionListener. Is it right?

据我所知,我创建了一个按钮,上面写着“清除”。然后我必须将一个动作与这个按钮相关联(如果按下按钮会发生什么),它是由addActionListener. 这样对吗?

But what I do not understand is where the action is specified. The press of the button should clear text area and, as far as I understand, there should be somewhere a code which clear the text area. But in the given example there is only "this" in the arguments of the addActionListener().

但我不明白的是指定操作的位置。按下按钮应该清除文本区域,据我所知,应该有一个代码可以清除文本区域。但是在给定的示例中,addActionListener().

How the program knows that it should clear the text area when the button is pressed?

程序如何知道按下按钮时它应该清除文本区域?

If it is needed, the full code is given here.

如果需要,这里给出了完整的代码。

采纳答案by McDowell

An ActionListeneris a callback mechanism. Whenever a control it is added to fires an ActionEvent, the public void actionPerformed(ActionEvent e)method will be invoked.

AnActionListener是一种回调机制。每当添加到的控件触发 时ActionEventpublic void actionPerformed(ActionEvent e)将调用该方法。

What I do not understand is where the actionPerformed is called. I see that it is defined within the class but there is no place where this method is called.

我不明白的是在哪里调用 actionPerformed。我看到它是在类中定义的,但没有调用此方法的地方。

This is called by the internal mechanisms of the UI component. Conceptually, you can think of the code looking a bit like this:

这是由 UI 组件的内部机制调用的。从概念上讲,您可以认为代码看起来有点像这样:

public class Button {
  private final List<ActionListener> listeners = new ArrayList<ActionListener>();

  public void addActionListener(ActionListener l) {
    listeners.add(l);
  }

  public void click() {
    ActionEvent event = new ActionEvent(this, 0, "click");
    for (ActionListener l : listeners) {
      l.actionPerformed(event);
    }
  }
}

回答by tangens

You call button.addActionListener( this ), because thisimplements the interface ActionListener. When the button is clicked, the method actionPerformed(ActionEvent e)(defined by the interface and implemented by your class) is called.

你调用button.addActionListener( this ),因为this实现了接口ActionListener。单击按钮时,将actionPerformed(ActionEvent e)调用该方法(由接口定义并由您的类实现)。

回答by trashgod

Each JButton, has an EventListenerList. Calling addActionListener(this)adds your ActionListener, a callback function named actionPerformed(), to the list. You can see an outline of the code that calls your method here. The actual fireActionPerformed()method is in AbstractButton. A JButtonis a JComponent, which you can see listedamong the various classes that use the event listener list mechanism.

每个JButton,都有一个EventListenerList。调用addActionListener(this)将您的ActionListener,一个名为 的回调函数添加actionPerformed()到列表中。您可以在此处查看调用您的方法的代码概要。实际fireActionPerformed()方法在AbstractButton. AJButton是 a JComponent,您可以看到它在使用事件侦听器列表机制的各种类中列出。

回答by Kameron

The event generator is told about the object which can handle its events Event Generators have a method; — addActionListener(reference to the object of Handler class) For example,

事件生成器被告知可以处理其事件的对象事件生成器有一个方法;——addActionListener(引用Handler类的对象)例如,

JButton b1=new JButton("My Button");
 b1.addActionListener(this); // Please note listener and generator are same class

Since event handler is in same object that contains button, so we have to use thisto pass the reference.

由于事件处理程序在包含按钮的同一个对象中,所以我们必须使用this来传递引用。

回答by CCNA

Basically the mechanism of UI event handling is the JVM queues events, and each type of event has its subscribers. When an event is fired, like the button is clicked, the JVM will accordingly delegate the processing to the event's subscriber. And this subscriber class has to define the method, or, event handler, to process the event.

基本上 UI 事件处理的机制是 JVM 对事件进行排队,并且每种类型的事件都有其订阅者。当一个事件被触发时,就像按钮被点击一样,JVM 会相应地将处理委托给事件的订阅者。这个订阅者类必须定义方法或事件处理程序来处理事件。

In your case, when calling button.addActionListener(this); the code actually subscribes this KeyEventDemo instance to the event of type click. Then, when the button is clicked, the KeyEventDemo's method of actionPerformed will be triggered.

在你的情况下,当调用 button.addActionListener(this); 该代码实际上将此 KeyEventDemo 实例订阅到类型为 click 的事件。然后,当按钮被点击时,KeyEventDemo 的 actionPerformed 方法将被触发。