如何以编程方式将 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
吗?
But isn't it possible to directly send it an ActionEvent
?
这不是生产代码,只是一个小小的个人实验.
This is not production code, it's just a little personal experiment.
可以获取一个按钮的ActionListener
,然后直接调用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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!