鼠标滚轮侦听器在jscrollbar中不起作用 [英] mouse wheel listener not working in jscrollbar

查看:126
本文介绍了鼠标滚轮侦听器在jscrollbar中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在netbeans平台(netbeans模块)中开发一个桌面应用程序,其中有一个桌面窗格和一个jscrollbar.我已经实现了MouseWheelListener 并添加了

I am developing a desktop application in netbeans platform (netbeans module) in which i have a desktoppane and a jscrollbar. i have implemented MouseWheelListener and added

scrollBar.addMouseWheelListener(this);

在类的构造函数中.现在,当我滚动鼠标滚轮时,尽管我正在

in the constructor of the class. now when i am scrolling the wheel of mouse it do not scroll the scroll bar though i am getting values in the

 private void scrollBarMouseWheelMoved(java.awt.event.MouseWheelEvent evt) { 


 System.out.println("mouse value is------------ " + evt.paramString());
}

上面的输出是

mouse value is------------ MOUSE_WHEEL,(8,49),absolute(0,0),button=0,clickCount=0,scrollType=WHEEL_UNIT_SCROLL,scrollAmount=3,wheelRotation=1

我现在应该怎么做才能在jscrollbar上启用mosue wheel事件?

what should i do now to enable mosue wheel event on jscrollbar?

我已经搜索了,但是找到了滚动窗格的事件,但是我正在显式地寻找滚动条.

I have searched but i found events for scrollpane but i am looking for scrollbar explicitly..

我删除了多余的代码,并在以下示例代码中显示了我要查找的内容

i have removed extra code and shown what i am looking for in the following sample code

    public final class ScrollableWindow1TopComponent extends TopComponent implements ComponentListener, MouseWheelListener {

    private javax.swing.JScrollBar scrollBar;
    private javax.swing.JDesktopPane scrollableGraphnewContainer;

    public ScrollableWindow1TopComponent() {
      this.addComponentListener(this);
      scrollBar.addMouseWheelListener(this);

    }
     private void scrollBarMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {                                          
       System.out.println("mouse value is------------ " + evt.paramString());
    }
     private void scrollBarAdjustmentValueChanged(java.awt.event.AdjustmentEvent evt) { 
      //code that works fine 
    }
  }

推荐答案

我的实际问题是我没有在更改鼠标滚轮时更改滚动值,但是却在获取鼠标滚轮事件,因此我解决了以下问题:

my actual problem was that i was not getting the scroll value changed on mouse wheel change but i was getting the mouse wheel event so i have solved as follow:

 private void scrollBarMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {                                          
        if (evt.getUnitsToScroll() > 0) {
            scrollBar.setValue(scrollBar.getValue() + 1);
        } else {
            scrollBar.setValue(scrollBar.getValue() - 1);
        }
   }      

每次我向上滚动滚轮时,evt.getUnitsToScroll()给出正值,向下滚动它给出负值,因此我增加了滚动条的值,其余的事情由

everytime i scroll wheel up side evt.getUnitsToScroll() gives positive value and for down it gives negative value so i incremented the value of scrollbar and rest of thing are automatically handled by

  private void scrollBarAdjustmentValueChanged(java.awt.event.AdjustmentEvent evt) { 
     //my code...
}

这篇关于鼠标滚轮侦听器在jscrollbar中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆