[英]Get mouse coordinates at any point on the screen
我試圖每次在屏幕上的任何位置移動鼠標時都獲取鼠標的坐標,然后記錄坐標,但是我對如何做卻有些迷失。
目前,我正在嘗試使用MouseListener,因此是否有必要在整個屏幕上進行覆蓋,使其透明,可點擊並可捕獲鼠標事件?
任何幫助表示贊賞,謝謝。
將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種方法
- mouseDragged
- mouseMoved
步驟3:將以下代碼放入mouseMoved(...),
在這里,僅出於說明目的,我已使用2標簽來顯示當前鼠標的位置。
@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
}
我的努力
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.