gpt4 book ai didi

Java 的 getWheelRotation() 总是返回 1 或 -1

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

我正在开发一个依赖于用户输入的程序,我需要的其中一件事是识别鼠标滚轮在一次滚动中向前或向后旋转了多少次“点击”的程序。

为此,我使用了 MouseWheelListener 和函数 getWheelRotation(),其中:

Returns the number of "clicks" the mouse wheel was rotated, as an integer.

但是,它似乎只返回 -1(如果旋转远离用户)或 1(如果旋转朝向它)。例如,如果轮子在远离我的地方旋转 3 次“咔嗒”,程序将返回 -1 而不是 -3 三次(当然,移动是连续进行的)。这是我用于该特定任务的代码:

private class Handler implements MouseListener, MouseWheelListener {

public void mouseWheelMoved(MouseWheelEvent event){
System.out.println(event.getWheelRotation());
}

//Some more code

}

我的代码有错误吗?如果不是,是否可以通过另一种方式实现,以便函数返回所需的值?

最佳答案

您可以尝试使用每次转动轮子时都会调用的计数函数,并在私有(private)成员变量中计算它被“点击”的频率。

private int counter = 0;

private count(int x) {
counter += x;
System.out.println("The wheel was turned " + counter + " times.");
}

private class Handler implements MouseListener, MouseWheelListener {

public void mouseWheelMoved(MouseWheelEvent event){
count(event.getWheelRotation());
}

//Some more code
}

“klicking”滚轮 3 次后的输出为:

The wheel was turned 1 times.
The wheel was turned 2 times.
The wheel was turned 3 times.

关于Java 的 getWheelRotation() 总是返回 1 或 -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23173779/

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