actionperfomed() 方法在哪里调用?

Where is the actionperfomed() method called?

我是 Java 初学者,现在当我开始使用界面时,我想知道到底发生了什么。我想一个很好的例子是 ActionListener 接口。

我对接口的了解是,它强制你实现接口给定的方法。但是我不明白,在哪里调用了 actionPerformed(ActionEvent e) 方法。是否有任何简单的示例可以向我展示后台发生的情况?还是谢谢了。

当用户执行某些操作时将调用它。点击一个按钮,select一个菜单。 actionListener 中的每一个都针对特定事件调用。我想现在,你不必担心它被调用的确切位置、位置和原因。

如果需要详细信息,请从内部记录异常 ActionListener#actionPerformed:

import java.io.*;
import javax.swing.*;
import java.awt.event.*;

public class ListenerTracer extends JPanel {

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() { createAndShowGUI(); }
        });
    }

    public ListenerTracer() {
        JButton b1 = new JButton("Press me");
        b1.setVerticalTextPosition(AbstractButton.CENTER);
        b1.setHorizontalTextPosition(AbstractButton.CENTER);
        b1.addActionListener(new ActionListener() {
            @Override public void actionPerformed(ActionEvent event) {
                Exception e = new Exception();
                e.printStackTrace();
            }
        });
        add(b1);

        JTextArea textArea = new JTextArea("actionListener printstacktrace:\n", 50, 50);
        JScrollPane scrollPane = new JScrollPane(textArea);
        add(scrollPane);
        Console.redirectOutput(textArea);
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("ListenerTracer");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ListenerTracer contentPane = new ListenerTracer();
        contentPane.setOpaque(true);
        frame.setContentPane(contentPane);
        frame.pack();
        frame.setVisible(true);
    }

    private static class Console implements Runnable {
        JTextArea displayPane;
        BufferedReader reader;

        private Console(JTextArea displayPane, PipedOutputStream pos) {
            this.displayPane = displayPane;
            try {
                PipedInputStream pis = new PipedInputStream(pos);
                reader = new BufferedReader( new InputStreamReader(pis) );
            }
            catch (IOException e) {}
        }

        public void run() {
            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    displayPane.append( line + "\n" );
                    displayPane.setCaretPosition(displayPane.getDocument().getLength());
                }
                System.err.println("im here");
            }
            catch (IOException ioe) {
                JOptionPane.showMessageDialog(null, "Error redirecting output : "+ioe.getMessage());
            }
        }

        public static void redirectOutput(JTextArea displayPane) {
            PipedOutputStream pos = new PipedOutputStream();
            System.setErr(new PrintStream(pos, true) );
            Console console = new Console(displayPane, pos);
            new Thread(console).start();
        }
    }
}

单击 "Press Me" 按钮会产生以下输出:

actionListener printstacktrace: java.lang.Exception at ListenerTracer.actionPerformed(ListenerTracer.java:21) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access0(Unknown Source) at java.awt.EventQueue.run(Unknown Source) at java.awt.EventQueue.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.run(Unknown Source) at java.awt.EventQueue.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)

我从camickr's answer to redirecting-system-out-to-jtextpane那里借用了控制台重定向代码。

对于 JButton,它的超类 AbstractButton 通过方法 fireActionPerformed 调用 actionPerformed 方法,这是一个受保护的方法。如果您查看源代码,您会发现它构造了一个 ActionEvent 对象并使用它作为参数调用 actionPerformed