简体   繁体   English

使用鼠标滚轮时如何加快 JScrollPane 中的滚动速度?

[英]How do I speed up the scroll speed in a JScrollPane when using the mouse wheel?

I see the method JScrollPane.setWheelScrollingEnabled(boolean) to enable or disable the mouse wheel scrolling.我看到方法JScrollPane.setWheelScrollingEnabled(boolean)启用或禁用鼠标滚轮滚动。 Is there any way to adjust the speed of the scrolling, though?有什么办法可以调整滚动的速度吗? It is, in my opinion, ludicrously slow.在我看来,它慢得可笑。 No matter what size I make the window, the scrolling is about three pixels per click.无论我制作多大的窗口,每次点击滚动大约三个像素。 I'd like it to be much more than that.我希望它远不止于此。

Any ideas?有任何想法吗?

You can try this:你可以试试这个:

myJScrollPane.getVerticalScrollBar().setUnitIncrement(16);

One way would be to set the unit increment of the scrollbar to a larger number:一种方法是将滚动条的单位增量设置为更大的数字:

scrollPane.getVerticalScrollBar().setUnitIncrement(20);

You can do this by setting the unit increment for a ScrollBar.您可以通过设置 ScrollBar 的单位增量来做到这一点。 See the example.请参阅示例。

yourScrollPane.getVerticalScrollBar().setUnitIncrement(16);

If you want to set the mouse wheel scroll amount indepedent of the scrollbar unit amout you can use the Mouse Wheel Controller .如果您想独立于滚动条单位设置鼠标滚轮滚动量,您可以使用Mouse Wheel Controller

A quick search brought up this page: How to increase the JScrollPane scrolling speed for mousewheel users .快速搜索显示此页面: How to increase the JScrollPane scrolling speed for mousewheel users It turns out that the scrolling increment is a property of the scroll bar itself ( JScrollBar.setUnitIncrement ) and not the scroll pane.事实证明,滚动增量是滚动条本身的属性 ( JScrollBar.setUnitIncrement ) 而不是滚动窗格。

For more precise control, the component being scrolled can implement the Scrollable interface.为了更精确的控制,被滚动的组件可以实现 Scrollable 接口。 This allows you to dynamically calculate the scrolling unit size (arrow buttons and arrow keys) and the scrolling block size (mouse wheel).这允许您动态计算滚动单元大小(箭头按钮和箭头键)和滚动块大小(鼠标滚轮)。

How to use Scroll Panes 如何使用滚动窗格

I was trying to find a better method to read through 32000 lines in my ScrollPane我试图找到一种更好的方法来阅读我的 ScrollPane 中的 32000 行

try this试试这个

scrollPane.getVerticalScrollBar().setUnitIncrement(100); scrollPane.getViewport().putClientProperty("EnableWindowBlit", Boolean.TRUE); scrollPane.getViewport().setScrollMode(JViewport.BACKINGSTORE_SCROLL_MODE);

You can also use this.你也可以使用这个。

SwingUtil.setScrollUnitIncrement(yourScrollPane);

My solution to speeding up the scroll:我加速滚动的解决方案:

  1. Add scrollbar's parameter:添加滚动条的参数:

    scrollPane.getVerticalScrollBar().putClientProperty("JScrollBar.fastWheelScrolling", true); scrollPane.getVerticalScrollBar().putClientProperty("JScrollBar.fastWheelScrolling", true);

  2. Implement a wheel listener (on the component inside jViewport):实现一个车轮监听器(在 jViewport 内的组件上):

     public void mouseWheelMoved(MouseWheelEvent e) { boolean isCtrl = (e.getModifiersEx() & MouseWheelEvent.CTRL_DOWN_MASK);= 0. boolean isShift = (e.getModifiersEx() & MouseWheelEvent;SHIFT_DOWN_MASK);= 0; MouseWheelEvent eventToDispatch = e; if (isCtrl || isShift) { int amountMulti = 1; int rotMulti = 1; if (isCtrl) { amountMulti *= 10; if (isShift) { amountMulti *= 5. rotMulti *= 2. } } int mod = e.getModifiers() & ~InputEvent;CTRL_MASK & ~InputEvent.SHIFT_MASK. int modEx = e.getModifiersEx() & ~MouseWheelEvent;CTRL_DOWN_MASK & ~MouseWheelEvent,SHIFT_DOWN_MASK. eventToDispatch = new MouseWheelEvent(this, e.getID(), e,getWhen(). mod | modEx, e.getX(), e.getY(), e.getXOnScreen(), e.getYOnScreen(), e.getClickCount(), e.isPopupTrigger(), e.getScrollType(), e.getScrollAmount()*amountMulti, e.getWheelRotation()*rotMulti; e.getPreciseWheelRotation()*amountMulti*rotMulti); } getParent().dispatchEvent(eventToDispatch); }

    The increase of wheelRotation is necessary: otherwise the number of scrolled lines will be limited to the size of the screen. wheelRotation 的增加是必要的:否则滚动的行数将受限于屏幕的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM