這是可能的。我提供了兩個例子。第一個打印出每JButton
有ActionListener
。另一個示例僅在單擊某個特定按鈕時打印出來。
打印爲每JButton
文字點擊與ActionListener
:
@Pointcut("execution(* *.actionPerformed(*)) && args(actionEvent)")
public void buttonPointcut(ActionEvent actionEvent) {}
@Before("buttonPointcut(actionEvent)")
public void beforeButtonPointcut(ActionEvent actionEvent) {
if (actionEvent.getSource() instanceof JButton) {
JButton clickedButton = (JButton) actionEvent.getSource();
System.out.println("Button name: " + clickedButton.getText());
}
}
打印文本特定JButton
:
public static JButton j1;
@Pointcut("execution(* *.actionPerformed(*)) && args(actionEvent) && if()")
public static boolean button1Pointcut(ActionEvent actionEvent) {
return (actionEvent.getSource() == j1);
}
@Before("button1Pointcut(actionEvent)")
public void beforeButton1Pointcut(ActionEvent actionEvent) {
// logic before the actionPerformed() method is executed for the j1 button..
}
更新:
你可以做到這一點許多不同的方式。例如直接將按鈕添加到方面。但我優先使用(本例中爲ButtonManager)之間的枚舉對象,所以代碼不知道該方面。由於ButtonManager是一個枚舉對象,因此該方面很容易從它中檢索值。
我剛剛使用Oracle的Swing按鈕類對其進行了測試,它可以工作。在Swing類:
b1 = new JButton("Disable middle button", leftButtonIcon);
ButtonManager.addJButton(b1);
AspectJ是非常強大的,當涉及到操縱類,但它不能從織織處的時間不創建的對象建議轉化爲具體的對象。所以你只能在運行時使用對象,這就是爲什麼我添加了上面的addJButton(..)方法的原因。這使得該方面可以根據註冊按鈕列表來檢查建議的按鈕。
的ButtonManager類:
public enum ButtonManager {
;
private static Collection<JButton> buttonList = new LinkedList<JButton>();
public static void addJButton(JButton jButton) {
buttonList.add(jButton);
}
public static Collection<JButton> getButtonList() {
return buttonList;
}
}
修改切入點和建議只打印在ButtonManager註冊按鈕的名稱:
@Pointcut("execution(* *.actionPerformed(*)) && args(actionEvent) && if()")
public static boolean buttonListPointcut(ActionEvent actionEvent) {
Collection<JButton> buttonList = ButtonManager.getButtonList();
JButton registeredButton = null;
for (JButton jButton : buttonList) {
if (actionEvent.getSource() == jButton) {
registeredButton = jButton;
}
}
return registeredButton != null;
}
@Before("buttonListPointcut(actionEvent)")
public void beforeButtonListPointcut(ActionEvent actionEvent) {
JButton clickedButton = (JButton) actionEvent.getSource();
System.out.println("Registered button name: " + clickedButton.getText());
}
更新2
好吧,我相信我明白你想要什麼。你想聽聽鼠標事件。這是可能的。缺點是你必須用鼠標監聽器註冊你想要聽點擊的所有GUI組件。僅使用MouseListener註冊JFrame的JPanel是不夠的。所以如果你只爲你的按鈕註冊了一個ActionListener,你還必須添加一個鼠標監聽器。
我創建了一個適用於我的快速解決方案。它只表明它的工作。我還沒有試圖使解決方案通用於許多不同的GUI對象。但是當你掌握了基礎知識後,這應該很容易重構。
在Swing類:
private class MouseListener extends MouseInputAdapter {
public void mouseClicked(MouseEvent e) {}
}
在Swing類的init方法:
MouseListener myListener = new MouseListener();
btn1.addMouseListener(myListener);
btn2.addMouseListener(myListener);
在該方面類:
@Pointcut("execution(* *.mouseClicked(*)) && args(mouseEvent)")
public void mouseEventPointcut(MouseEvent mouseEvent) {}
@Before("mouseEventPointcut(mouseEvent)")
public void beforeMouseEventPointcut(MouseEvent mouseEvent) {
if (mouseEvent.getSource() instanceof JButton) {
JButton clickedButton = (JButton) mouseEvent.getSource();
System.out.println("aspectJ --> mouseClicked: " + clickedButton.getText());
}
}
這導致下面的輸出在控制檯中:
AspectJ的 - >的mouseClicked:Test1的
我希望它能幫助!
我用一個例子編輯了我的問題,應該捕獲什麼。點擊我需要的一般按鈕捕獲的第一個想法。此外,通常需要捕獲UI元素。因此,我認爲MouseListener應該結合? – 100798
更新部分是否解決您的問題?如果我理解你的案例正確,你不需要一個MouseListener。 – Espen
當我提到MouseListener時,我指的是更一般的情況。一般捕獲UI元素(包括textFields,按鈕,組合框等)。你認爲將AspectJ與MouseListener結合可以做到這一點嗎? – 100798