JPanel 中的鼠标移动事件
我在使鼠标事件正常工作时遇到一些问题。我在 JLayeredPane 内部有一个 JPanel,它位于 JScrollPane 中。诚然,我对使用 Swing 相当陌生,但本质上,我希望 JPanel 对鼠标移动做出反应,但一直无法让它工作。
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");}
}
任何帮助将不胜感激,提前致谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否正在使用 MouseListener 注册您的 JPanel 对象?
比如:
或者您可能需要将 MouseListener 添加到 ScrollPane 或 LayeredPane 中?
Are you registering your JPanel Object with the MouseListener?
Something like:
Or maybe you need to add the MouseListener to The ScrollPane or LayeredPane?
以下是一些您可以使用的演示代码:
Here's some demo code you can play with: