鼠标退出java时的锁定事件

发布于 2024-10-01 03:20:33 字数 225 浏览 1 评论 0原文

我编写了一个应用程序,您可以在其中左右拖动一些时间线。 当我拖动时,我知道这是一个 mouseDragged 事件。由于我将鼠标光标移到组件之外,它会停止拖动,但我不希望这样。

所以我想以某种方式锁定该组件上的 mouseDragged 事件,但是如果我将光标移到组件之外,我将不再收到 mouseDragged 事件。

我该如何解决这个问题?我的同事告诉我有一些像captureMouse这样的功能。

I wrote an application where you can drag left and right some timeline.
When i do dragging i know that this is a mouseDragged event. Since i move mouse cursor outside component it stops dragging, but i don't want that.

So i want to somehow lock mouseDragged event on that component, but if i move cursor outside component, i don't get mouseDragged event anymore.

How can i solve that? My colleague tell me that there exists some function like captureMouse.

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

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

发布评论

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

评论(3

春风十里 2024-10-08 03:20:33

我不认为捕获老鼠是个好主意。我也不认为这对于你想要的东西是必要的。

根据我的经验,即使您将鼠标移到组件区域之外,mouseDragged() 事件也能正常工作:它将继续报告组件内最接近组件外鼠标位置的 X 和 Y 位置,但它赢了不要像释放左键一样终止鼠标拖动。

如果您的应用程序的行为不同,也许您的鼠标处理中有不同的错误?

例如,尝试一下本页中间的小程序:您将看到可以开始拖动矩形并将鼠标移到小程序区域之外,它将继续工作:
http://www.dgp.toronto.edu/~mjmcguff/ learn/java/04-mouseInput/

(我不知道上面代码的作者;这只是我通过搜索 java applet mousedragged 找到的第一个代码。)

I don't think it's such a good idea to capture the mouse. I also don't think it's necessary for what you want.

In my experience, mouseDragged() events work fine even when you move the mouse outside of the component area: it will continue to report the X and Y positions inside the component that are closest to the mouse's location outside of the component, but it won't terminate the mouse drag as if you had released the left button.

If the behaviour of your application is different, maybe you've got a different bug in your mouse handling?

For example, try out the applet half way down this page: you'll see that you can start dragging the rectangle and move your mouse outside of the applet area, and it will continue to work:
http://www.dgp.toronto.edu/~mjmcguff/learn/java/04-mouseInput/

(I don't know the author of the code above; it's just one of the first I found with a search of java applet mousedragged.)

肥爪爪 2024-10-08 03:20:33

“时间线”的哪一部分是接收 mouseDragged() 事件的实际组件?通常,在接收到此事件时,您将移动组件以匹配拖动操作 - 这样,组件应始终位于鼠标下方并始终接收 mouseDragged() 事件,直到您放开它。您在处理事件时是否正在移动此组件?用作滑块的类似按钮的小矩形将是一个很好用的组件。

(如果您尝试将整个时间轴视为组件,因此不移动它来匹配,那么如果用户在外面拖动鼠标,您希望发生什么?)

尽管 Windows(可能还有其他)支持捕获鼠标, Java 似乎没有。您也许可以提供一个自定义事件队列,如下所示:

Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener()
    {
        public void eventDispatched(AWTEvent e)
        {
            // Look for mouse messages and handle them... 
        }

    }
, AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK);

Which part of your 'timeline' is the actual component receiving the mouseDragged() event? Normally on receiving this event you would move the component to match the dragging action - that way, the component should always be under the mouse and always receive the mouseDragged() event until you let go of it. Are you moving this component as you process the events? A small button-like rectangle used as a slider would make an excellent component to use.

(If you are trying to treat your whole timeline as the component and are therefore not moving it to match, what do you want to happen if the user drags the mouse while outside?)

Although Windows (and possibly others) support capturing the mouse, Java does not appear to. You might be able to provide a custom event queue something like this:

Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener()
    {
        public void eventDispatched(AWTEvent e)
        {
            // Look for mouse messages and handle them... 
        }

    }
, AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK);
伪装你 2024-10-08 03:20:33
JComponent.setAutoScrolls(...);
JComponent.setAutoScrolls(...);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文