所以這裏是我正在使用的整個文件,並在絕望中,我把x和y定義在每一個事件中,但我仍然沒有得到任何東西..這是一個多類的項目,我應該在哪裏做addMouseListener()
?爲什麼沒有收到這個事件呢? (注意:run()
工程中的座標打印。)這是一個小程序項目,該小程序正常工作,但未註冊此事件。點擊檢測不起作用?
public class ClickCheck implements Runnable, java.awt.event.MouseListener {
public int x, y;
public ClickCheck() {
Thread t = new Thread(this);
t.start();
}
public void mouseClicked(java.awt.event.MouseEvent ev) {
x = ev.getX();
y = ev.getY();
System.out.println(x + "," + y);
}
public void mousePressed(java.awt.event.MouseEvent ev) {
x = ev.getX();
y = ev.getY();
System.out.println(x + "," + y);
}
public void mouseReleased(java.awt.event.MouseEvent ev) {
x = ev.getX();
y = ev.getY();
System.out.println(x + "," + y);
}
public void mouseEntered(java.awt.event.MouseEvent ev) {
x = ev.getX();
y = ev.getY();
System.out.println(x + "," + y);
}
public void mouseExited(java.awt.event.MouseEvent ev) {
x = ev.getX();
y = ev.getY();
System.out.println(x + "," + y);
}
public void run() {
System.out.println(x + "," + y);
}
}
請將代碼放在你調用addMouseListener的地方。如果您不確定在哪裏調用它,請將偵聽器添加到您希望用戶點擊的圖形組件中。 –