本文介绍了如何以编程方式将 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 ActionListeners, 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-12 03:44