سؤال

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?

هل كانت مفيدة؟

المحلول

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?

  • then JFrame lost Focus, you can test by using WindowFocusListener

  • Focus is asynchronous, then everything inside windowGainedFocus and windowLostFocus should be wrapped into 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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top