如何以编程方式将ActionEvent发送到JButton? [英] How do I programatically send ActionEvent to JButton?

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆