以编程方式单击Java Swing中的GUI按钮 [英] Programmatically clicking a GUI button in Java Swing

查看:401
本文介绍了以编程方式单击Java Swing中的GUI按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何以编程方式单击Swing JButton,以便注册所有相关的动作/鼠标事件并且对用户可见(即他们看到按钮被按下就像他们实际点击它一样)?

How would I programmatically click a Swing JButton in a way that would register all the relevant action/mouse events and be visible to the user (i.e. they'd see the button being pressed as if they actually clicked it)?

该按钮位于我正在运行的同一个应用程序中;我不是想控制另一个应用程序中的按钮。我想我可以直接将事件注入队列,但是如果可能的话我宁愿避免使用这种方法,这样做也不会显示可见的点击。

The button is in the same application I'm running; I'm not trying to control a button in another application. I suppose I could directly inject events into the queue, but I'd prefer to avoid that approach if possible, and doing it that way wouldn't show a visible click.

我看到java.awt.Robot类提供了移动鼠标并单击鼠标的方法,但没有让它单击特定按钮。

I see the java.awt.Robot class offers methods to move the mouse and click the mouse, but not to make it click a particular button.

推荐答案

您是否尝试过使用 doClick()

这篇关于以编程方式单击Java Swing中的GUI按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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