make enter key就像java中jtextfield,jcombobox,jspinner的tab键一样

时间:2013-09-19 08:28:44

标签: java swing focus key-bindings

我希望回车键在我的swing应用程序中表现得像tab键。这个类适用于JTextFields.How我可以为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();
      }
    });
  }
}

1 个答案:

答案 0 :(得分:2)

输入对于Swing中的大多数组件都有特殊含义,例如{k}> 时,JTextField会在已注册的actionPerformed上触发ActionListeners被压了。修改此行为可能会对您的应用程序产生意外结果,并可能使许多用户感到困惑......

话虽如此,更改焦点遍历键的最佳方法是向Set提供KeyStroke KeyboardFocusManager秒。这将(主要)使密钥成为全局。

某些组件提供了自己的焦点遍历键,例如JTextAreaJTable

请查看How to use 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);
        }
    }

}