简体   繁体   English

如何检查是否从JFrame中按下了鼠标

[英]How to check if the mouse is pressed out of an JFrame

现在,我正在使用MouseListener来查看是否按下了鼠标,但是当您在JFrame外部按下时它没有注册,因此我真的需要这样做吗,如何检查JFrame外部的鼠标事件?

Right now I am using a MouseListener to see if the mouse is pressed but it doesn't register when you press outside of an JFrame I would really need it to so how do I check for mouse events outside of a JFrame? 现在,我正在使用MouseListener来查看是否按下了鼠标,但是当您在JFrame外部按下时它没有注册,因此我真的需要这样做吗,如何检查JFrame外部的鼠标事件?

  • then JFrame lost Focus , you can test by using WindowFocusListener 然后JFrame失去了Focus ,您可以使用WindowFocusListener进行测试

  • Focus is asynchronous, then everything inside windowGainedFocus and windowLostFocus should be wrapped into invokeLater 焦点是异步的,因此windowGainedFocuswindowLostFocus内部的windowGainedFocus windowLostFocus都应包装到invokeLater

Add a window listener 添加一个窗口监听器

addWindowListener(new WindowListener() {

        @Override
        public void windowOpened(WindowEvent arg0) {


        }

        @Override
        public void windowIconified(WindowEvent arg0) {


        }

        @Override
        public void windowDeiconified(WindowEvent arg0) {


        }

        @Override
        public void windowDeactivated(WindowEvent arg0) {


        }

        @Override
        public void windowClosing(WindowEvent arg0) {

        }

        @Override
        public void windowClosed(WindowEvent arg0) {

        }

        @Override
        public void windowActivated(WindowEvent arg0) {


        }
    });

Try out all the methods (window...) and see which one works out best for you! 试用所有方法(窗口...),看看哪种方法最适合您! :) I'm not telling you exactly what to do because to learn you cant just copy paste! :)我没有告诉您确切的操作,因为要学习就不能复制粘贴!

To know the status of mouse outside the window you can use: 要了解窗口外的鼠标状态,可以使用:

Point point = MouseInfo.getPointerInfo().getLocation();

Unfortunatly java.awt.event.MouseMotionListener give you information about mouse movement inside your window. 不幸的是, java.awt.event.MouseMotionListener提供了有关鼠标在窗口内移动的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM