JScrollPane 使用箭头键滚动

2022-09-04 08:23:26

我在JScrollPane中有一个JTextArea组件,文本区域不可编辑。我想使用向上和向下箭头键启用文本区域的滚动(即按箭头键将文本区域滚动一行)。任何想法如何实现这一目标?


答案 1

是的,密钥绑定是要走的路,但您并不总是需要创建自己的操作。Swing 组件附带了您通常可以重复使用的默认操作。

有关这些操作的完整列表,请参阅密钥绑定

现在您已经知道了 Action 名称,只需将其绑定到键行程即可:

JScrollBar vertical = scrollPane.getVerticalScrollBar();
InputMap im = vertical.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
im.put(KeyStroke.getKeyStroke("DOWN"), "positiveUnitIncrement");
im.put(KeyStroke.getKeyStroke("UP"), "negativeUnitIncrement");

答案 2

如果 JTextArea 不可编辑且不可聚焦,则它不会响应箭头键。我不确定是否有一种规范的方法来解决这个问题,但是让它响应的一种方法是设置其键绑定,以便在JTextArea处于可聚焦窗口中时响应向上和向下键。这方面的一个例子如下:

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.*;
import javax.swing.text.JTextComponent;

@SuppressWarnings("serial")
public class TestScrollingArea extends JPanel {
    private static final String UP = "Up";
    private static final String DOWN = "Down";
    private JTextArea area = new JTextArea(20, 40);
    private JScrollPane scrollPane = new JScrollPane(area);

    public TestScrollingArea() {
        // make textarea non-editable and non-focusable
        area.setEditable(false);
        area.setFocusable(false);
        area.setWrapStyleWord(true);
        area.setLineWrap(true);
        add(scrollPane);

        // fill area with letters
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 100; j++) {
                area.append("abcdefg ");
            }
        }

        // have JTextArea tell us how tall a line of text is.
        int scrollableIncrement = area.getScrollableUnitIncrement(scrollPane.getVisibleRect(), 
                    SwingConstants.VERTICAL, 1);

        // add key bindings to the JTextArea 
        int condition = JTextComponent.WHEN_IN_FOCUSED_WINDOW;
        InputMap inMap = area.getInputMap(condition);
        ActionMap actMap = area.getActionMap();

        inMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), UP);
        inMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), DOWN);
        actMap.put(UP, new UpDownAction(UP, scrollPane.getVerticalScrollBar().getModel(), 
                    scrollableIncrement));
        actMap.put(DOWN, new UpDownAction(DOWN, scrollPane.getVerticalScrollBar().getModel(), 
                    scrollableIncrement));

    }

    // Action for our key binding to perform when bound event occurs
    private class UpDownAction extends AbstractAction {
        private BoundedRangeModel vScrollBarModel;
        private int scrollableIncrement;
        public UpDownAction(String name, BoundedRangeModel model, int scrollableIncrement) {
            super(name);
            this.vScrollBarModel = model;
            this.scrollableIncrement = scrollableIncrement;
        }

        @Override
        public void actionPerformed(ActionEvent ae) {
            String name = getValue(AbstractAction.NAME).toString();
            int value = vScrollBarModel.getValue();
            if (name.equals(UP)) {
                value -= scrollableIncrement;
                vScrollBarModel.setValue(value);
            } else if (name.equals(DOWN)) {
                value += scrollableIncrement;
                vScrollBarModel.setValue(value);
            }
        }
    }

    private static void createAndShowUI() {
        JFrame frame = new JFrame("TestScrollingArea");
        frame.getContentPane().add(new TestScrollingArea());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                createAndShowUI();
            }
        });
    }
}

推荐