JPanel中的鼠标移动事件 [英] Mouse moved event within JPanel

查看:329
本文介绍了JPanel中的鼠标移动事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的鼠标事件无法正常工作.我在JScrollPane中的JLayeredPane中有一个JPanel.诚然,我对使用Swing还是比较陌生,但从本质上讲,我希望JPanel对鼠标的移动做出反应,但一直无法使其正常工作.

I'm having some issues with getting my mouse events to work. I have a JPanel inside of a JLayeredPane which is in a JScrollPane. Admittedly, I am fairly new to working with Swing, but essentially, I want the JPanel to react to the mouse moving, but have been unable to get it to work.

public class CellHighlighter extends JPanel implements MouseMotionListener{

    public CellHighlighter(){

    }

    public void mouseMoved(MouseEvent evt){
        System.out.println(evt.getPoint().x + ", " + evt.getPoint().y);
    }

    public void mouseDragged(MouseEvent evt){System.out.println("message");}

}

任何帮助将不胜感激,在此先感谢您!

Any help would be much appreciated, thanks in advance!

推荐答案

您是否正在使用MouseListener注册JPanel对象? 像这样:

Are you registering your JPanel Object with the MouseListener? Something like:

    public CellHighlighter(){
       this.addMouseMotionListener(this);
    }

或者您可能需要将MouseListener添加到ScrollPane或LayeredPane?

Or maybe you need to add the MouseListener to The ScrollPane or LayeredPane?

这篇关于JPanel中的鼠标移动事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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