gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:17:56 26 4
gpt4 key购买 nike

我是一名 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com