JButton 动作监听器

发布于 2024-09-08 01:08:56 字数 85 浏览 2 评论 0原文

嘿那里,我正在寻找一个按键监听器,当按下鼠标时它总是会激活自己...例如,如果我继续按下按钮,它总是会在控制台上写一些东西,并在我释放鼠标按钮时停止写东西。

Hey there, I'm searching for a Key Listener which always activates himself when the mouse is pressed down... for example if I keep pressing the button it will always write something on the console and stops writing that when I releas the mouse button.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别闹i 2024-09-15 01:08:56

首先,KeyListeners 响应键盘事件:MouseListeners 响应鼠标事件。但是,如果您正在处理 JButton,您可能需要将 ActionListener 附加到该按钮。当按下 JButton 时,它将做出响应,这可以以不同的方式进行,不涉及鼠标。

听众一般不会做这种事。您需要的是一个计时器,它会以一定的时间间隔向控制台重复写入一些内容。然后,您使用 ActionListener 在按下按钮时启动该进程,并在释放按钮时停止该进程。这些类的文档应该可以满足您的需要。

Firstly, KeyListeners respond to keyboard events: MouseListeners respond to mouse events. However if you are dealing with a JButton you probably want an ActionListener attached to the button. That will respond when the JButton is pressed, which can be in different ways, not involving the mouse.

Listeners don't generally do that sort of thing. What you need is a Timer that will write something to the console repeatedly at some interval. Then you use a ActionListener to start the process going when the button is pressed, and stop it when the button is released. The documentation for those classes should give you what you need.

眼眸 2024-09-15 01:08:56

您可以使用 MouseListener 来做到这一点,它向应用程序发出信号开始在 MousePressed 上打印某些内容,并发出信号以停止在 MouseReleased 上打印。

您应该在单独的线程中进行打印,以防止 Swing GUI 冻结。

You could do that with a MouseListener which signals the app to start printing something on MousePressed, signals to stop printing on MouseReleased.

You should do the printing in a separate thread as to prevent the Swing GUI from freezing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文