获取JFrame的鼠标位置,即使其子组件处于焦点状态 [英] Get mouse position of a JFrame even when its child component is focused

查看:66
本文介绍了获取JFrame的鼠标位置,即使其子组件处于焦点状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取JFrame的鼠标位置.但是,当鼠标位于其子组件(例如表,添加到JFrame的按钮)上时,MouseMotion事件不再监听.如何获得鼠标位置?

I want to get the mouse position of my JFrame. But when mouse is on its child component (such as a table, button that is added to the JFrame) MouseMotion event is no more listening. How can I get the mouse position?

推荐答案

假设注释中的用例是要解决的 real 问题,答案是实现mouseExited,以便它进行检查鼠标是否仍在框架上方,仅在不隐藏时才隐藏.

Assuming the use-case in your comment is the real issue to solve, the answer is to implement the mouseExited such that it checks whether the mouse is still somewhere over the frame and hide it only if not.

类似的东西:

MouseListener closer = new MouseAdapter() {

    @Override
    public void mouseExited(MouseEvent e) {
        Rectangle r = new Rectangle(sideBar.getSize());
        if (!r.contains(e.getPoint())) {
            sideBar.setVisible(false);
        }
    }

};

这篇关于获取JFrame的鼠标位置,即使其子组件处于焦点状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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