Detecting mouse enter/exit events anywhere on JPanel

前端 未结 5 1772
北海茫月
北海茫月 2020-12-06 05:36

Basically there is a JPanel on which I want to know when the mouse enters the area of the JPanel and exits the area of the JPanel. So I added a mouse listener, but if there

5条回答
  •  我在风中等你
    2020-12-06 06:06

    A simpeler solution with java 1.8+

    public class MyJPanel implements MouseListener {
    
        public void mouseExited(MouseEvent e) {        
            if(!this.contains(e.getPoint())) {
                ... //the rest of your code
            }
        }
    
        ...
    }
    

提交回复
热议问题