1

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

4

2 回答 2

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);
                        }
                    }
                }
            });
        }
    }
于 2013-01-23T00:22:04.420 回答
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

于 2013-01-22T18:41:39.987 回答