我在让鼠标事件正常工作时遇到了一些问题。我在 JScrollPane 中的 JLayeredPane 中有一个 JPanel。不可否认,我对使用 Swing 还很陌生,但本质上,我希望 JPanel 对鼠标移动使用react,但一直无法让它工作。
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");}
}
非常感谢任何帮助,提前致谢!
最佳答案
您是否正在使用 MouseListener 注册您的 JPanel 对象? 像这样的东西:
public CellHighlighter(){
this.addMouseMotionListener(this);
}
或者您可能需要将 MouseListener 添加到 The ScrollPane 或 LayeredPane?
关于java - JPanel 中的鼠标移动事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8439086/