开发者

How do I Click a JButton without the user Clicking it?

I have a JButton and when a player clicks it it tell my Action Listener that a button is c开发者_如何转开发licked. What i want to know is that is there a command or something that acts as if a player clicked the button.

Like Tic Tac Toe, i have it so 2 players can play against each other, but i want to add the option for a computer player vs a human player. But since the computer cant actually click the button, i am lost.

Edit: would it be as easy as gridButton2.click() (Name of button).click();


Pretty much. All you need to do is use the doClick() function. See the API for more information.


for the tic-tac-toe thing, you don't need the computer to click a button. you just have to wait until the human makes a move, then have the computer choose its move and execute the code that happens if the button gets clicked.


Just call directly the actionPerformed() method from the ActionListenerimplementing class. You can do it the following way:

actionPerformed(new ActionEvent(gridButton2, ActionEvent.ACTION_FIRST, "youractioncommand"));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜