【问题标题】:How to change Java Swing JScrollPane mouse wheel priority to horizontal scrolling?如何将 Java Swing JScrollPane 鼠标滚轮优先级更改为水平滚动?
【发布时间】:2013-01-22 17:14:45
【问题描述】:

我现在可以看到滚动优先级是垂直滚动。我想改变它。我该怎么做?

【问题讨论】:

  • 对于 JTextComponents、JList 或 JTable 或 ....,(以这种形式)这个问题无法回答

标签: java swing jscrollpane


【解决方案1】:

感谢第一个答案-非常有帮助。但是我发现上面响应中的 iNewValue 需要乘以 evt.getWheelRotation() 值,该值是滚轮鼠标实际旋转的不同滚轮鼠标段的数量。

滚动的时间条件也需要考虑到这一点 - 条件必须是 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);
                        }
                    }
                }
            });
        }
    }

【讨论】:

    【解决方案2】:

    要更改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 的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-20
      • 2011-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多