在 Java 中,我的 MouseMotionListener 和 MouseListener 正在检测点击,但没有检测到移动

In Java, my MouseMotionListener and MouseListener is detecting clicks, but not movement

所以我正在创建一个 java 游戏,这里检测到我的鼠标点击,但 mouseMoved 根本没有 运行。方法中有一条永远不会得到 运行 的打印语句。我真的被卡住了,因为点击是在 window 上注册的,鼠标移动应该没有任何问题。

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

谢谢。

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了解更多详情