想要告诉我层次结构中的哪个组件正在执行操作的 javax.swing 钩子 [英] Want javax.swing hook that tells me WHICH component in the hierarchy is executing an action

查看:18
本文介绍了想要告诉我层次结构中的哪个组件正在执行操作的 javax.swing 钩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以最少的代码丑化在 Swing 程序中编写一个调试挂钩,它会告诉我层次结构中的哪个组件实际处理每个 KeyStroke 或鼠标单击并执行在组件的动作映射中映射到它的动作?我们正在编写一个复杂的 GUI,了解这些信息将非常有用.

How with minimum code uglification can I write a debugging hook in a Swing program that would tell me which component in the hierarchy is actually handling each KeyStroke or mouse click and performing the action mapped to it in the component's action map? We are writing a complicated GUI and it would be very useful to know this information.

推荐答案

可以使用 AspectJ 将日志方面编织到您的代码库中.下面是在执行任何对象时使用您的代码库中具有的方法 actionPerformed(ActionEvent) 的连接点的建议示例.类似的结构可用于建议其他听众.

Using AspectJ to weave a logging aspect into your code base can be done. Below is an example of advice on a joinpoint within execution of any objects with the method actionPerformed(ActionEvent) you have in your code base. Similar constructions can be used to advise other Listeners.

下面是建议按钮按下和其他具有 ActionListeners 的组件的方面.它只是输出动作来源的类名和 actionPerformed 方法的签名.

Below is the aspect to advise button presses and other components having ActionListeners. It simply outputs the class name of the source of the action and the signature of the actionPerformed method.

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 中):

A test class which produces two buttons of different classes (in file 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();
      }
    });
  }
}

这篇关于想要告诉我层次结构中的哪个组件正在执行操作的 javax.swing 钩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆