有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java JScrollPane使用箭头键滚动

我在JScrollPane中有一个JTextArea组件,文本区域是不可编辑的。我想使用上下箭头键启用文本区域的滚动(即按下箭头键将文本区域滚动一行)。有什么办法可以做到这一点吗


共 (4) 个答案

  1. # 1 楼答案

    如果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();
                }
            });
        }
    }
    
  2. # 2 楼答案

    是的,键绑定是一种方法,但您并不总是需要创建自己的操作。Swing组件带有默认操作,您可以经常重用这些操作

    有关这些操作的完整列表,请参见Key Bindings

    现在您已经知道了操作名称,只需将其绑定到击键:

    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");
    
  3. # 3 楼答案

    您应该将KeyListener添加到JScrollPane中

  4. # 4 楼答案

    刚遇到这个问题,虽然答案有助于推动我走向正确的方向,但从那时起,解决方案中的一些方面可能已经发生了变化。它对我起了作用,有以下变化: -必须更改的是JScrollPane实例的InputMap -actionMapKeys必须是:“unitScrollX”和/或“scrollX”(X=下、上、左、右)。他们居住在巴西斯克罗尔帕内伊