public class App extends JFrame implements KeyListener {
private JLabel lblNewLabel;
private String str = "";
public App() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(400, 300);
setResizable(false);
getContentPane().setLayout(null);
lblNewLabel = new JLabel("");
lblNewLabel.setBounds(0, 259, 394, 13);
getContentPane().add(lblNewLabel);
setVisible(true);
new Thread() {
public void run() {
while (true) {
try {
Point mainPoint = getLocation();
Point point = MouseInfo.getPointerInfo().getLocation();
int x = point.x - mainPoint.x;
int y = point.y - mainPoint.y;
if (x < 0 || x > 400) {
sleep(2);
continue;
}
if (y < 0 || y > 300) {
sleep(2);
continue;
}
lblNewLabel.setText(str + "x : " + x + " y : " + y);
sleep(2);
} catch (Exception e) {
}
}
}
}.start();
this.addKeyListener(this);
}
public static void main(String[] args) {
new App();
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_CONTROL) {
str = "C ";
return;
}
if (e.getKeyCode() == KeyEvent.VK_SHIFT) {
str = "S ";
return;
}
str = "D ";
}
public void keyReleased(KeyEvent e) {
str = "U ";
}
public void keyTyped(KeyEvent e) {
}
}
你这个代码我跑起来左下角是有个鼠标的坐标的啊