2

我想为我的JTextField. 但看起来箭头键绑定到JScrollPane我放置文本字段的位置。我如何解绑它们?

4

1 回答 1

6

您可以尝试替换滚动窗格上的键绑定,但保留它们以允许用户在未集中在您的文本字段中时滚动窗格可能是有意义的。

相反,您可以将键绑定添加到不执行任何操作的文本字段,这将消耗事件并阻止它们开始发送到滚动窗格,例如...。

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.Scrollable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestScrollPaneKeyBinding {

    public static void main(String[] args) {
        new TestScrollPaneKeyBinding();
    }

    public TestScrollPaneKeyBinding() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new JScrollPane(new TestPane()));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel implements Scrollable {

        public TestPane() {
            JTextField field = new JTextField(20);
            InputMap im = field.getInputMap();
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "Arrow.up");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "Arrow.down");
            AbstractAction doNothing = new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    // doing nothing
                }
            };
            ActionMap am = field.getActionMap();
            am.put("Arrow.up", doNothing);
            am.put("Arrow.down", doNothing);
            add(field);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.dispose();
        }

        @Override
        public Dimension getPreferredScrollableViewportSize() {
            return new Dimension(100, 100);
        }

        @Override
        public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
            return 32;
        }

        @Override
        public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
            return 32;
        }

        @Override
        public boolean getScrollableTracksViewportWidth() {
            return false;
        }

        @Override
        public boolean getScrollableTracksViewportHeight() {
            return false;
        }
    }

}
于 2013-09-06T07:53:54.053 回答