Java-MouseWheelEvent触发两次

Java-MouseWheelEvent触发两次,java,swing,jframe,Java,Swing,Jframe,我正在使用swing JFrame,该程序的一个功能是使用鼠标滚轮缩放窗口中的图像。我已经实现了一个MouseAdapter,它被添加为JFrame本身的MouseWheelListener /** * Handles scroll wheel activity. */ private MouseAdapter wheelListener = new MouseAdapter() { @Override public void mouseWheelMoved(MouseWhe

我正在使用swing JFrame,该程序的一个功能是使用鼠标滚轮缩放窗口中的图像。我已经实现了一个MouseAdapter,它被添加为JFrame本身的MouseWheelListener

/**
 * Handles scroll wheel activity.
 */
private MouseAdapter wheelListener = new MouseAdapter() {
    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        int notches = e.getWheelRotation();
        System.out.println(notches);
        while (notches > 0) {
            controller.zoomIn();
            notches--;
        }
        while (notches < 0) {
            controller.zoomOut();
            notches++;
        }
    }
};
我遇到的问题是,滚动滚轮时,每次“单击”事件都会触发两次。我找不到有类似问题的人

我尝试在
mouseweelmoved
方法中放入
if(e.getScrollType()==mouseweeleEvent.WHEEL\u UNIT\u SCROLL){…}
,以查看是否发生了两种不同类型的事件,但它们都是
WHEEL\u UNIT\u SCROLL的

我还试着打印出事件的来源,看看它是否来自不同的窗口/窗格,但它们都来自我的主JFrame窗口

有没有人知道,或者说,为什么我要参加两项比赛,而我应该参加一项

编辑:对不起,在“添加控制盘侦听器”部分中放置了错误的行。 编辑:经过一些测试,我能够使用
.hashCode()
验证是否存在两个唯一的
mouseweelevents
。我怀疑MouseAdapter不知何故被添加了两次。我将它添加到大型机的构造函数中,并验证它只在那里发生一次。

添加一个e.consume()解决了这个问题

private MouseAdapter wheelListener = new MouseAdapter() {
    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        e.consume() // avoid the event to be triggred twice
        int notches = e.getWheelRotation();
        System.out.println(notches);
        while (notches > 0) {
            controller.zoomIn();
            notches--;
        }
        while (notches < 0) {
            controller.zoomOut();
            notches++;
        }
    }
};
private MouseAdapter wheelstener=new MouseAdapter(){
@凌驾
已移动公共无效鼠标滚轮(鼠标滚轮事件e){
e、 consume()//避免触发两次事件
int槽口=e.getWheelRotation();
系统输出打印LN(槽口);
而(槽口>0){
controller.zoomIn();
缺口--;
}
而(槽口<0){
controller.zoomOut();
缺口++;
}
}
};

当我在我的系统中尝试您的代码时,它运行良好。是否多次添加侦听器?考虑创建和发布一个演示您的问题的方法。此外,当您的问题与Windows监听器无关时,为什么要向我们展示<代码> AddiToWistListNER(…)>代码位?<代码>“我怀疑MouthAdditer在某种程度上被添加了两次。”< /代码>:正如我所怀疑的。你也没有显示有问题的代码。如果我知道有问题的代码在哪里,我就能修复它,对吗?有没有办法让侦听器在添加时识别它?如果是这样的话,我可以抛出一个异常来跟踪它……它必须在代码中,您需要做的就是在代码中搜索添加这些类型侦听器的位置。
private MouseAdapter wheelListener = new MouseAdapter() {
    @Override
    public void mouseWheelMoved(MouseWheelEvent e) {
        e.consume() // avoid the event to be triggred twice
        int notches = e.getWheelRotation();
        System.out.println(notches);
        while (notches > 0) {
            controller.zoomIn();
            notches--;
        }
        while (notches < 0) {
            controller.zoomOut();
            notches++;
        }
    }
};