如何将Java Swing JScrollPane鼠标滚轮优先级更改为水平滚动? [英] How to change Java Swing JScrollPane mouse wheel priority to horizontal scrolling?

查看:58
本文介绍了如何将Java Swing JScrollPane鼠标滚轮优先级更改为水平滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在可以看到滚动优先级是垂直滚动.我想改变那个.我该怎么办?

I can see right now the scrolling priority is given to vertical scrolling. I want to change that. How do I do that?

推荐答案

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

Thanks to the first answer- very helpful. However I found that iNewValue in the response above needs to be multiplied by the evt.getWheelRotation() value which is the number of distinct wheel mouse segments actually rotated by the wheel mouse.

滚动时间的条件也需要考虑到这一点-条件必须是evt.getWheelRotation()< = -1或evt.getWheelRotation()> = 1

Also the condition of when to scroll needs to take account of this as well - the condition has to be evt.getWheelRotation() <= -1 or evt.getWheelRotation() >= 1

这是为我工作的更新示例.

Here is an updated example that worked for me.

    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);
                        }
                    }
                }
            });
        }
    }

这篇关于如何将Java Swing JScrollPane鼠标滚轮优先级更改为水平滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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