如果没有显式调用'actionPerformed'方法怎么调用呢?

Sai*_*S.R 10 java methods events user-interface swing

我刚开始用Swing学习GUI,并不完全理解该actionPerformed方法的工作原理.请考虑以下代码:

//code to create a button and change its text when clicked
public class simplegui implements ActionListener {
  JButton button;

  public static void main(String[] args) {
    simplegui gui=new simplegui();
    gui.go();
  }

  public void go() {
    JFrame frame=new Frame();
    button=new JButton("click Me");
    button.addActionListener(this);

    frame.getContentPane().add(button);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300,300);
    frame.setVisible(true);
  }

  public void actionPerformed(ActionEvent event) {
    button.setText("I've been clicked!");
  }
}
Run Code Online (Sandbox Code Playgroud)

在引发方法之前,不应该为类创建对象(静态方法除外)?

单击该按钮时会actionPerformed调用该方法,但如何?电话在哪里?我已经实现了接口ActionListener,但是代码知道当一个动作发生时,'ActionEvent'对象应该被发送到'actionPerformed'方法?它出现在Button类中吗?该addActionListener方法是否存在于Button类中?

单击按钮时,系统调用操作如何执行以及执行的代码在gui.actionPerformed()哪里?

我一直遵循旧的OO,静态等Java概念,但这整个事件驱动编程令人困惑.

Sha*_*y D 11

每个事件都由一个对象表示,该对象提供有关事件的信息并标识事件源.事件源通常是组件或模型,但其他类型的对象也可以是事件源.

在这里,您注册的听众,即

button.addActionListener(this);
Run Code Online (Sandbox Code Playgroud)

被添加到侦听器列表中,当JVM收到事件(在这种情况下单击)时,它会在列表中的所有侦听器上调用相应的方法.

这是怎么发生的?好吧,我认为你应该阅读Callbackjava中的机制.

您还可以使用回调机制创建自己的侦听器.考虑以下代码:

该代码是一个信用卡应用simulation.In下面的代码,该pinChanged()方法被调用自动changePin()方法被调用.

public interface PinChangeListener {
    public void pinChanged();
}

public class CreditCard {
    public PinChangeListener pinChangeListener;

    private int pin;

    public changePin(int pin) {
        this.pin = pin;
        if (pinChangeListener != null) {
            pinChangeListener.pinChanged();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

要将回调/监听器连接到信用卡,您只需要实现PinChangeListener方法:

creditCard.pinChangeListener = new PinChangeListener() {
    public void pinChanged() {
        System.out.println("The pin has been changed");
    }
};
Run Code Online (Sandbox Code Playgroud)

类似地,当您将监听器附加到按钮时,JVM会检测到单击,(您可能不希望进入检测到单击的方式!)并且actionPerformed()JVM为您调用附加的监听器.希望这清楚.


Hov*_*els 5

但是这个方法有一个特定的调用,只是它不是发生在您的代码中,而是发生在 JVM 中。按钮按下会引发内部事件,导致 JVM 告诉按钮通知所有侦听器它已被按下。这将导致调用所有附加的 ActionListener 的 actionPerformed 方法。

要了解有关其工作原理的信息,请首先查看AbstractButton 类的 Java API,您将在其中找到该方法

protected void fireActionPerformed(ActionEvent event)
Run Code Online (Sandbox Code Playgroud)

在哪里

通知所有已注册对此事件类型的通知感兴趣的侦听器。事件实例是使用事件参数延迟创建的。

然后,要了解更多信息,您将需要超越 Java API 来了解源代码,可以在此处找到源代码。如果您查看那里的 Java 8.0 源代码,查找 javax,然后查找 swing,然后查找 AbstractButton,您会找到一个fireActionPerformed(ActionEvent event)方法:

2002    protected void More ...fireActionPerformed(ActionEvent event) {
2003        // Guaranteed to return a non-null array
2004        Object[] listeners = listenerList.getListenerList();
2005        ActionEvent e = null;
2006        // Process the listeners last to first, notifying
2007        // those that are interested in this event
2008        for (int i = listeners.length-2; i>=0; i-=2) {
2009            if (listeners[i]==ActionListener.class) {
2010                // Lazily create the event:
2011                if (e == null) {
2012                      String actionCommand = event.getActionCommand();
2013                      if(actionCommand == null) {
2014                         actionCommand = getActionCommand();
2015                      }
2016                      e = new ActionEvent(AbstractButton.this,
2017                                          ActionEvent.ACTION_PERFORMED,
2018                                          actionCommand,
2019                                          event.getWhen(),
2020                                          event.getModifiers());
2021                }
2022                ((ActionListener)listeners[i+1]).actionPerformed(e);
2023            }
2024        }
2025    }
Run Code Online (Sandbox Code Playgroud)

  • 作为参考,这种方法在 [`EventListenerList`](http://docs.oracle.com/javase/8/docs/api/javax/swing/event/EventListenerList.html) API 中概述,[此处]( http://stackoverflow.com/q/2159803/230513)。 (2认同)