java - actionperformed() 方法在哪里调用?

标签 java interface

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

我对接口(interface)的了解是,它迫使您实现接口(interface)给出的方法。但我不明白的是,在哪里调用了 actionPerformed(ActionEvent e) 方法。有没有简单的例子可以告诉我后台发生了什么?不管怎样,谢谢。

最佳答案

如果您需要详细信息,请从 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$2.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.access$400(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.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 借用了控制台重定向代码.

关于java - actionperformed() 方法在哪里调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29392336/

相关文章:

java - 这里面的时间复杂度是多少?

java - 以编程方式创建的 EJB 计时器在集群中执行的频率如何?

typescript - 如何定义对象的接口(interface),其每个字段必须是 TypeScript 中其他接口(interface)的实例?

java - 脂肪 jar 的动态负载

java - 如何正确检测、解码和播放 radio 流?

java - 如何在不调用多个 Ajax Web 服务的情况下继续查询表

java - Collections.sort() 不适用于实现 Comparable 的类或使用 Comparator Java 时

java - 从另一个类的函数的返回值分配对象值?

pointers - golang 异常规则 "interface pointer can' t 实现接口(interface)”

javascript - 使用 Javascript 调用 Flash (.swf) 文件中的方法?