我正在尝试编写一个程序,当我单击鼠标左键时,它会显示 x 和 y 坐标,但它不起作用。如果我运行程序,控制台会自动显示 x 和 y 坐标,但我想在单击鼠标后查看坐标。
import java.awt.*;
import java.awt.event.*;
import javax.swing.SwingUtilities;
public class Simple {
public static void main(String[] args){
Mouse maus = new Mouse();
maus.mouseClicked(null);
}
}
第二个文件
import java.awt.*;
import java.awt.event.*;
public class Mouse implements MouseListener {
public void mouseClicked(MouseEvent e) {
double mouseX = MouseInfo.getPointerInfo().getLocation().getX();
double mouseY = MouseInfo.getPointerInfo().getLocation().getY();
System.out.println(mouseX+" "+mouseY);
}
@Override
public void mouseEntered(MouseEvent arg0) {}
@Override
public void mouseExited(MouseEvent arg0) {}
@Override
public void mousePressed(MouseEvent arg0) {}
@Override
public void mouseReleased(MouseEvent arg0) {}
}