如何以编程方式将 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
吗?
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);
}
相关文章