gpt4 book ai didi

java - 在java中不断获取鼠标相对于Jframe的位置

转载 作者:行者123 更新时间:2023-11-30 06:49:44 25 4
gpt4 key购买 nike

我正在创建一个java应用程序,我需要获取每一帧鼠标相对于我的Jframe的位置。我正在编写一个从 MouseAdapter 扩展的类,但在需要时无法访问 MouseEvent。

谢谢。

最佳答案

使用 Swing 计时器:打印到 JFrame 左上边缘的相对距离。

public static void main(String[] args) {
JFrame frame = new JFrame("Frame");
frame.setSize(500, 500);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);

Timer timer = new Timer(66, new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
Point p = MouseInfo.getPointerInfo().getLocation();
p = new Point(p.x - frame.getLocation().x, p.y - frame.getLocation().y);
System.out.println("Mouse: " + p);

}
});
timer.start();

}

关于java - 在java中不断获取鼠标相对于Jframe的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43063768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com