【发布时间】:2016-03-19 04:48:03
【问题描述】:
我试图在每次鼠标在屏幕上的任意点移动时获取鼠标的坐标,然后记录坐标,但我对如何做有点迷茫。
目前,我正在尝试使用 MouseListener,那么有没有办法在整个屏幕上制作一个透明、可点击并能够捕获鼠标事件的叠加层?
感谢您的帮助。
【问题讨论】:
标签: java mouseevent coordinates mouse overlay
我试图在每次鼠标在屏幕上的任意点移动时获取鼠标的坐标,然后记录坐标,但我对如何做有点迷茫。
目前,我正在尝试使用 MouseListener,那么有没有办法在整个屏幕上制作一个透明、可点击并能够捕获鼠标事件的叠加层?
感谢您的帮助。
【问题讨论】:
标签: java mouseevent coordinates mouse overlay
将 MouseMotionListener 与 mouseDragged 方法一起使用,例如:
public void mouseDragged(MouseEvent e) {
Graphics g = this.getGraphics();
int x = e.getX(), y = e.getY(); // you have the coordinates
// if you want to draw a line for example between 2 mouse pos:
g.drawLine(lastX,lastY,x,y);
lastX =x;
lastY =y;
}
【讨论】:
您需要遵循以下步骤,
第 1 步: implements MouseMotionListener 到您的班级的接口,
步骤:2:覆盖它的 2 方法
- 鼠标拖拽
- 鼠标移动
步骤:3:将以下代码放入mouseMoved(...),
在这里,仅出于解释目的,我采用了 2-label 这将 显示当前鼠标的位置。
@Override
public void mouseMoved(MouseEvent e) {
String xvalue = new Integer(e.getX()).toString();
String yvalue = new Integer(e.getY()).toString();
xlable.setText(xvalue); // here , you can write it into you log
ylable.setText(yvalue); // here , you can write it into you log
}
我努力的成果,
【讨论】: