本文介紹了如何以編程方式將 ActionEvent 發(fā)送到 JButton?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
如何以編程方式將 ActionEvent
(例如按鈕按下/ACTION_PERFORMED)發(fā)送到 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);
但是不能直接給它發(fā)送一個(gè)ActionEvent
嗎?
But isn't it possible to directly send it an ActionEvent
?
這不是生產(chǎn)代碼,只是一個(gè)小小的個(gè)人實(shí)驗(yàn).
This is not production code, it's just a little personal experiment.
推薦答案
可以獲取一個(gè)按鈕的ActionListener
,然后直接調(diào)用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);
}
這篇關(guān)于如何以編程方式將 ActionEvent 發(fā)送到 JButton?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!