想要javax.swing钩子告诉我层次结构中的WHICH组件正在执行操作

|| 如何以最小的代码丑化程度在Swing程序中编写调试钩子,以告诉我层次结构中的哪个组件实际上在处理每个KeyStroke或鼠标单击并执行在组件的动作图中映射到它的动作?我们正在编写一个复杂的GUI,了解此信息将非常有用。     
已邀请:
        放入自定义事件分配器: http://tips4java.wordpress.com/2009/09/06/global-event-dispatching/ 还要在API文档中查找AWTEvent.getID(),MouseEvent,KeyEvent。 这就是我使用的软件监视鼠标和键盘的方式,以查看用户是否忙于工作,如果不是,则锁定窗口。     
        使用AspectJ可以将日志记录方面编织到代码库中。以下是在代码库中使用方法“ 0”执行任何对象的连接点建议示例。类似的构造可用于建议其他侦听器。 以下是建议按钮按下和具有ActionListeners的其他组件的方面。它仅输出动作源的类名和actionPerformed方法的签名。
import java.awt.event.ActionEvent;

import org.aspectj.lang.Signature;

public aspect Logger {
  before(ActionEvent e) : execution(* *.actionPerformed(ActionEvent)) && args(e) {
    Signature sig = thisJoinPoint.getSignature();
    System.out.println(e.getSource().getClass() + \" lead to \" + sig);
  }
}
一个测试类,它产生两个不同类的按钮(在文件StackTraceExample.java中):
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

class MyButton extends JButton {
  public MyButton(String string) {
    super(string);
  }
}

class MyOtherButton extends JButton {
  public MyOtherButton(String string) {
    super(string);
  }
}

class ButtonStackDisplay implements ActionListener {
  private final JTextArea stackTraceText;

  ButtonStackDisplay(JTextArea textArea) {
    this.stackTraceText = textArea;
  }

  public void actionPerformed(ActionEvent e) {
    String endl = System.getProperty(\"line.separator\");
    StringBuilder b = new StringBuilder();

    // you can see the source of the event 
    b.append(e.getSource()).append(endl).append(endl);

    // the stack trace shows that events don\'t propagate through the components
    // originating them, but instead processed in a different thread
    for (StackTraceElement se : new Throwable().getStackTrace()) {
      b.append(se.toString());
      b.append(endl);
    }
    stackTraceText.setText(b.toString());
  }
}

public class StackTraceExample {
  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.setLayout(new BorderLayout());
        JPanel top = new JPanel();
        JButton button = new MyButton(\"Stack Trace\");
        top.setLayout(new GridLayout(2, 1));
        top.add(button);
        JButton otherButton = new MyOtherButton(\"Stack Trace\");
        top.add(otherButton);
        f.getContentPane().add(top, BorderLayout.NORTH);
        JTextArea stackTraceText = new JTextArea();
        f.add(stackTraceText, BorderLayout.CENTER);
        ButtonStackDisplay bsd = new ButtonStackDisplay(stackTraceText);
        button.addActionListener(bsd);
        otherButton.addActionListener(bsd);
        f.setSize(400, 400);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
        f.toFront();
      }
    });
  }
}
    

要回复问题请先登录注册