0

我希望输入键在我的 Swing 应用程序中表现得像 Tab 键。这个类对于 JTextFields 工作正常。我如何为 JComboBox 和 Jspinner 或框架上的其他控件做同样的事情?请帮助。

class MyTextField extends JTextField {
    MyTextField(int len) {
    super(len);
    addKeyListener(new KeyAdapter() {
            @Override
      public void keyPressed(KeyEvent evt) {
        int key = evt.getKeyCode();
        if (key == KeyEvent.VK_ENTER)
          transferFocus();
      }
    });
  }
}
4

1 回答 1

2

Enter对于 Swing 中的大多数组件都有特殊的含义,例如在 按下时会在注册时JTextField触发。修改此行为可能会给您的应用程序带来意想不到的结果,并且可能会使许多用户感到困惑......actionPerformedActionListenersEnter

话虽如此,更改焦点遍历键的最佳方法是Set为. 这将(主要)使密钥成为全球性的。KeyStrokeKeyboardFocusManager

但是,某些组件的供应有自己的焦点遍历键,例如JTextAreaJTable

查看如何使用 Focus Subsystem了解更多详情

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestFocusTraversal {

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

    public TestFocusTraversal() {
        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 TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            for (int index = 0; index < 10; index++) {
                JTextField tf = new JTextField(5);
                tf.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("...");
                    }
                });
                add(tf);
            }

            add(new JScrollPane(new JTextArea(10, 10)));

            KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
            KeyStroke tab = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0);
            KeyStroke ctrlTab = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, KeyEvent.CTRL_DOWN_MASK);
            Set<KeyStroke> keys = new HashSet<>();
            keys.add(enter);
            keys.add(tab);
            keys.add(ctrlTab);
            KeyboardFocusManager.getCurrentKeyboardFocusManager().setDefaultFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, keys);
        }
    }

}
于 2013-09-19T08:54:07.120 回答