2013-01-22 88 views

回答

0

要改變,你需要創建自己的版本JScrollPane的默認滾動優先JScrollPane當鼠標滾輪移動執行水平ScrollBar進來的行動,而不是垂直ScrollBar

JScrollPane中的重寫版本可以這樣創建:

import javax.swing.JScrollPane; 
import javax.swing.JScrollBar; 
import java.awt.Component; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseWheelListener; 
import java.awt.event.MouseWheelEvent; 

class MyJScrollPane extends JScrollPane 
{ 
    public MyJScrollPane(Component component) 
    { 
     super(component); 
     final JScrollBar horizontalScrollBar = getHorizontalScrollBar(); 
     final JScrollBar verticalScrollBar = getVerticalScrollBar(); 
     setWheelScrollingEnabled(false); 
     addMouseWheelListener(new MouseAdapter() 
     { 
      public void mouseWheelMoved(MouseWheelEvent evt) 
      { 
       if (evt.getWheelRotation() == 1)//mouse wheel was rotated down/ towards the user 
       { 
        int iScrollAmount = evt.getScrollAmount(); 
        int iNewValue = horizontalScrollBar.getValue() + horizontalScrollBar.getBlockIncrement() * iScrollAmount; 
        if (iNewValue <= horizontalScrollBar.getMaximum()) 
        { 
         horizontalScrollBar.setValue(iNewValue); 
        } 
       } 
       else if (evt.getWheelRotation() == -1)//mouse wheel was rotated up/away from the user 
       { 
        int iScrollAmount = evt.getScrollAmount(); 
        int iNewValue = horizontalScrollBar.getValue() - horizontalScrollBar.getBlockIncrement() * iScrollAmount; 
        if (iNewValue >= 0) 
        { 
         horizontalScrollBar.setValue(iNewValue); 
        } 
       } 
      } 
     }); 
    } 
} 

我希望這能解決您的JScrollPane滾動優先的問題。

2

感謝第一個答案 - 非常有幫助。然而,我發現上述響應中的iNewValue需要乘以evt.getWheelRotation()值,該值是滾輪鼠標實際旋轉的不同滾輪鼠標段的數量。

另外的時要滾動的需求考慮到這個問題,以及條件 - 的條件必須是evt.getWheelRotation()< = -1或evt.getWheelRotation()> = 1

下面是一個更新的例子,爲我工作。

import java.awt.Component; 
    import java.awt.event.MouseAdapter; 
    import java.awt.event.MouseWheelEvent; 
    import javax.swing.JScrollBar; 
    import javax.swing.JScrollPane; 
    class MyJScrollPane extends JScrollPane 
    { 



     public MyJScrollPane(Component component) 
     { 
      super(component); 
      final JScrollBar horizontalScrollBar = getHorizontalScrollBar(); 
      final JScrollBar verticalScrollBar = getVerticalScrollBar(); 
      setWheelScrollingEnabled(false); 
      addMouseWheelListener(new MouseAdapter() 
      { 
       public void mouseWheelMoved(MouseWheelEvent evt) 
       { 

        if (evt.getWheelRotation() >= 1)//mouse wheel was rotated down/ towards the user 
        { 
         int iScrollAmount = evt.getScrollAmount(); 
         int iNewValue = horizontalScrollBar.getValue() + horizontalScrollBar.getBlockIncrement() * iScrollAmount * Math.abs(evt.getWheelRotation()); 
         if (iNewValue <= horizontalScrollBar.getMaximum()) 
         { 
          horizontalScrollBar.setValue(iNewValue); 
         } 
        } 
        else if (evt.getWheelRotation() <= -1)//mouse wheel was rotated up/away from the user 
        { 
         int iScrollAmount = evt.getScrollAmount(); 
         int iNewValue = horizontalScrollBar.getValue() - horizontalScrollBar.getBlockIncrement() * iScrollAmount * Math.abs(evt.getWheelRotation()); 
         if (iNewValue >= 0) 
         { 
          horizontalScrollBar.setValue(iNewValue); 
         } 
        } 
       } 
      }); 
     } 
    }