gpt4 book ai didi

java - ActionListener 与 AbstractAction

转载 作者:行者123 更新时间:2023-11-30 08:40:43 25 4
gpt4 key购买 nike

<分区>

我正在尝试了解有关事件处理的更多信息,但无论我读到什么,它主要是关于如何使用它以便发生某些事情,而不是它如何工作。

到目前为止,我知道有两种方法可以在单击按钮时让事情发生。

Action 监听器:

myButton.addActionListener(new ActionListener() { @override actionPerformed... });

和抽象 Action :

public class MyAction extends AbstractAction {
public MyAction(String text, ImageIcon icon, String desc, Integer mnemonic) {
super(text, icon);
putValue(SHORT_DESCRIPTION, desc);
putValue(MNEMONIC_KEY, mnemonic);
}
public void actionPerformed(ActionEvent e) {
System.out.println("Action", e);
}
}

MyAction myAction = new MyAction(...);
myButton.setAction(myAction);

我知道我可以将我想要发生的一切写入actionPerfomed() 方法。

但由于我不知道,在后台究竟发生了什么,我无法判断一个是否比另一个有任何优势,或者我应该在哪种情况下使用哪个?

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