gpt4 book ai didi

java - 在Java中,我的MouseMotionListener和MouseListener正在检测点击,但不检测移动

转载 作者:行者123 更新时间:2023-12-01 22:25:06 26 4
gpt4 key购买 nike

所以我正在创建一个java游戏,这里检测到了我的鼠标点击,但是mouseMoved根本没有运行。该方法中有一条永远不会运行的打印语句。我真的很困惑,因为由于点击是在整个窗口中注册的,所以鼠标运动不应该有任何问题。

鼠标是否依赖于其他任何东西,因为我真的不知道是什么原因造成的。

谢谢。

public class Mouse implements MouseListener, MouseMotionListener{

private static int mouseX = -1;
private static int mouseY = -1;
private static int mouseB = -1;

public static int getX(){
return mouseX;
}

public static int getY(){
return mouseY;
}

public static int getB(){
return mouseB;
}



public void mouseMoved(MouseEvent e) {
System.out.println("Mouse Moved");
mouseX = e.getX();
mouseY = e.getY();
}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mousePressed(MouseEvent e) {
mouseB = e.getButton();
System.out.println(e.getX() + " " + e.getY());
}

public void mouseReleased(MouseEvent e) {
mouseB = -1;
}

public void mouseDragged(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {

}
}

这是我的游戏构造函数,它启动了一切。

public Game(){
//cCursor();

screen = new Screen(WIDTH, HEIGHT);
mouse = new Mouse();
keys = new Keyboard();
//level = new RandomLevel(64, 64);
level = Level.spawn;
TileCoord pSpawn = new TileCoord(20,66);
player = new Player(pSpawn.x(), pSpawn.y(), keys);
player.init(level);

addKeyListener(keys);
addMouseListener(mouse);
}

最佳答案

MouseMoitionListener 是与 MouseListener 不同的监听器,需要单独注册...

首先添加对 addMouseMotionListener 的调用

addMouseListener(mouse);
addMouseMotionListener(mouse);

看看How to Write a Mouse Listener了解更多详情

关于java - 在Java中,我的MouseMotionListener和MouseListener正在检测点击,但不检测移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28935663/

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