Java -- 如何为 JScrollPane 设置键盘滚动速度 [英] Java -- How to set the keyboard scroll speed for a JScrollPane

查看:27
本文介绍了Java -- 如何为 JScrollPane 设置键盘滚动速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

A JPanel 有一个 JScrollPane 包含另一个 JPanel 或两个.我的生活取决于使用键盘的方向箭头提高滚动速度.经过仔细考虑,决定的权力:sc.getVerticalScrollBar().setUnitIncrement(240); 应该只适用于鼠标,巧妙地招致java开发人员的小烦恼.有什么可以提高滚动速度的方法吗?我的生活悬而未决.

A JPanel has a JScrollPane that contains yet another JPanel or two. My life depends on increasing the scroll speed using a keyboard's directional arrows. After careful deliberation, the powers that be decided that: sc.getVerticalScrollBar().setUnitIncrement(240); should only be applicable to a mouse, in a clever ruse to elicit minor annoyances amongst java developers. Is there anything that can be done to increase scroll speed? My life hangs in the balance.

推荐答案

必须结合使用 InputMap.putActionMap.put 捕获 JScrollPane 中包含的组件的键盘事件,并在 JScrollPane 获得焦点时处理键盘事件.由于滚动的默认增量值为 1,您应该将所需的增量值添加或减去 JScrollPane 的滚动条当前值,您可以使用 JScrollPane.getVerticalScrollBar().getValue() 并使用 JScrollPane.getVerticalScrollBar().setValue(int).

You have to use a combination of InputMap.put and ActionMap.put to capture the keyboard events for the components contained on your JScrollPane and process the keyboard events when the JScrollPane has the focus. Since the default increment value for scrolling is 1 you should add or substract the desired increment value to the current value of the scrollbar for JScrollPane which you can get with JScrollPane.getVerticalScrollBar().getValue() and set with JScrollPane.getVerticalScrollBar().setValue(int).

使用 JScrollPane 捕获包含元素的事件的示例可以使用此代码完成,我已经使用按钮完成了,但您明白了(抱歉代码组织不当):

An example of capturing events for the contained elements withing JScrollPane can be done with this code, I've done with buttons, but you get the point (Sorry for the bad organization of the code):

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test
{
    public static void main(String[] args)
    {
        final JFrame f = new JFrame("");
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(2000,1));


        for(int  i = 0; i != 2000; i++)
        {
            JButton btn = new JButton("Button 2");
            panel.add(btn);
        }
        final JScrollPane sPane = new JScrollPane(panel);
        final int increment = 5000;
        sPane.getVerticalScrollBar().setUnitIncrement(increment);

        KeyStroke kUp = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0);
        KeyStroke kDown = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0);

        sPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(kUp,"actionWhenKeyUp");
        sPane.getActionMap().put("actionWhenKeyUp",
            new AbstractAction("keyUpAction")
            {
                public void actionPerformed(ActionEvent e)
                {
                    final JScrollBar bar = sPane.getVerticalScrollBar();
                    int currentValue = bar.getValue();
                    bar.setValue(currentValue - increment);
                }
            }
        );

        sPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(kDown,"actionWhenKeyDown");
        sPane.getActionMap().put("actionWhenKeyDown",
            new AbstractAction("keyDownAction")
            {
                public void actionPerformed(ActionEvent e)
                {
                    final JScrollBar bar = sPane.getVerticalScrollBar();
                    int currentValue = bar.getValue();
                    bar.setValue(currentValue + increment);
                }
            }
        );
        f.add(sPane);
        f.pack();



        SwingUtilities.invokeLater(
                new Runnable()
                {
                    public void run()
                    {
                        f.setVisible(true);
                    }
                }
            );
    }
}

我们注册以监听和处理该事件:

We register to listen and process that event with:

sPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(kUp,"actionWhenKeyUp");
sPane.getActionMap().put("actionWhenKeyUp",
    new AbstractAction("keyUpAction")
    {
        public void actionPerformed(ActionEvent e)
        {
            final JScrollBar bar = sPane.getVerticalScrollBar();
            int currentValue = bar.getValue();
            bar.setValue(currentValue - increment);
        }
    }
);

执行 JScrollBar 增量值的关键代码是 AbstractAction(在这种情况下,当用户按下向上键时).

The key code that perform the value of JScrollBar increment is of the AbstractAction (in this case when the user press the up key).

        public void actionPerformed(ActionEvent e)
        {
            final JScrollBar bar = sPane.getVerticalScrollBar();
            int currentValue = bar.getValue();
            bar.setValue(currentValue - increment);
        }

您应该做的是在 JScrollPane 获得焦点时完成事件,但这应该是微不足道的.

What you should do is to complete the events when your JScrollPane has the focus, but that should be trivial.

希望它有助于挽救您的生命:P 或至少为您提供一个起点.

Hope it helps to save your life :P or at least serve you as a starting point.

这篇关于Java -- 如何为 JScrollPane 设置键盘滚动速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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