如何将鼠标锁定在JFrame中 [英] How do I lock the mouse inside a JFrame

查看:59
本文介绍了如何将鼠标锁定在JFrame中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将鼠标锁定在JFrame中.也就是说,鼠标不能离开JFrame的内容(除非用户单击转义,alt-tab或否则会失去焦点的窗口).想法?

I would like to lock the mouse inside a JFrame. That is, the mouse can not leave the contents of the JFrame (unless the user hits escape, alt-tab, or the window otherwise looses focus). Ideas?

谢谢!

推荐答案

我不确定是否有更自动的方法,但是您可以使用

I'm not sure if there's a more automatic way of doing that, but you could use the Robot class to set the mouse position. So in the event handler for when the JFrame gains focus you can start watching the mouse move event, and when the mouse moves just make sure it stays within the JFrame. If it leaves the JFrame you can use the Robot class to set the mouse's position to go back.

然后,当窗口失去焦点时,您可以从鼠标移动事件中注销.

Then when the window loses focus, you can unregister from the mouse move event.

这篇关于如何将鼠标锁定在JFrame中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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