0

以下代码应该让程序检测何时在某个 JTextField(称为 timeStep)中按下选项卡并在 TextArea 中显示一条消息(称为 textAreaInsructions),但它似乎不起作用。谁能告诉我为什么?

timeStep.setFocusTraversalKeys(
                KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, Collections.EMPTY_SET);

        timeStep.addKeyListener(new KeyAdapter() {
            public void keyPressed(KeyEvent evt) {
                if(evt.getKeyCode() == KeyEvent.VK_TAB){
                    instruction = "Enter a real number time step";
                    textAreaInstructions.setText(instruction);
                    /* If you want to change the focus to the next component */
                    //nextJComponent.grabFocus();
            }
            }
        });
4

1 回答 1

1

TAB 键已经被大多数挥杆组件吃掉了,因为它与场焦点控制相关联。处理它的方法不止一种,您可以尝试其他方法:

您可以使用 FocusListener 来简单地检测焦点何时离开文本字段,而不是显式地监听 TAB 键。

或者

注册一个键盘动作: JComponent.registerKeyboardAction(action, keystroke, WHEN_IN_FOCUSED_WINDOW); 对于 TAB 键。

于 2013-10-30T16:52:22.603 回答