如何将鼠标锁定在JFrame中 [英] How do I lock the mouse inside a 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屋!