如何以编程方式将ActionEvent发送到JButton? [英] How do I programmatically send ActionEvent to JButton?
本文介绍了如何以编程方式将ActionEvent发送到JButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何以编程方式将 ActionEvent
(例如按下按钮/ ACTION_PERFORMED)发送到 JButton
?
How do I programmatically send an ActionEvent
(eg button pressed/ACTION_PERFORMED) to a JButton
?
我知道:
button.doClick(0);
和
button.getModel().setArmed(true);
button.getModel().setPressed(true);
button.getModel().setPressed(false);
button.getModel().setArmed(false);
但是不可能直接发送 ActionEvent
?
编辑:这不是生产代码,只是一个小小的个人实验。
This is not production code, it's just a little personal experiment.
推荐答案
你可以得到一个按钮的 ActionListener
s,然后调用 actionPerformed
方法直接。
You can get a button's ActionListener
s, and then call the actionPerformed
method directly.
ActionEvent event;
long when;
when = System.currentTimeMillis();
event = new ActionEvent(button, ActionEvent.ACTION_PERFORMED, "Anything", when, 0);
for (ActionListener listener : button.getActionListeners()) {
listener.actionPerformed(event);
}
这篇关于如何以编程方式将ActionEvent发送到JButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文