KeyListener问题

时间:2015-08-27 01:38:41

标签: java swing keylistener

我有一个实现KeyListener的小类,但即使我实现了所有方法,包括keyPressed(),并且给了它们所有代码来运行,当我按下enter(return)键时没有任何反应。

这是我实现KeyListener的类(实现的方法在底部):

public class TestPanel extends JPanel implements KeyListener {
Words word = new Words();
private JLabel rootLabel;
JLabel rootField;
private JLabel defLabel;
JTextField defField;
private JButton ok;
TestListener listener;
private JLabel correctLabel;
private JLabel incorrectLabel;
private int correctCount = 0;
private int incorrectCount = 0;
ActionListener okListener = new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        if (defField.getText().equals("")) {

        } else {

            String root = word.getRoot();
            String def = word.getDef();
            boolean status;
            if (defField.getText().equals(word.getDef())) {
                correctCount++;
                correctLabel.setText("Correct: " + correctCount);
                status = true;
            } else {
                incorrectCount++;
                incorrectLabel.setText("Incorrect: " + incorrectCount);
                status = false;
            }

            defField.setText("");
            word.setRootAndDef();
            rootField.setText(word.getRoot());
            TestEvent event = new TestEvent(ok, root, def, status);
            listener.dataSubmitted(event);

        }

    }

};

public TestPanel() {
    word = new Words();
    rootLabel = new JLabel("Root: ");
    rootField = new JLabel();
    defLabel = new JLabel("Definition: ");
    defField = new JTextField(20);
    ok = new JButton("OK");
    correctLabel = new JLabel("Correct: " + correctCount);
    incorrectLabel = new JLabel("Incorrect: " + incorrectCount);

    ok.setMnemonic(KeyEvent.VK_O);

    setBorder(BorderFactory.createTitledBorder("Test"));

    ok.addActionListener(okListener);

    setLayout(new GridBagLayout());

    GridBagConstraints gc = new GridBagConstraints();

    // First row

    gc.weightx = 1;
    gc.weighty = 0.2;
    gc.gridx = 0;
    gc.gridy = 0;
    gc.anchor = GridBagConstraints.LINE_END;
    gc.insets = new Insets(0, 0, 0, 5);

    add(rootLabel, gc);

    gc.gridx = 1;
    gc.anchor = GridBagConstraints.LINE_START;
    gc.insets = new Insets(0, 0, 0, 0);

    add(rootField, gc);

    // Next row

    gc.gridx = 0;
    gc.gridy = 1;
    gc.anchor = GridBagConstraints.LINE_END;
    gc.insets = new Insets(0, 0, 0, 5);

    add(defLabel, gc);

    gc.gridx = 1;
    gc.gridy = 1;
    gc.anchor = GridBagConstraints.LINE_START;
    gc.insets = new Insets(0, 0, 0, 0);

    add(defField, gc);

    // Next row

    gc.weighty = 2;
    gc.gridx = 1;
    gc.gridy = 2;
    gc.anchor = GridBagConstraints.FIRST_LINE_START;

    add(ok, gc);

    // Next row

    gc.weighty = 0.1;
    gc.weightx = 1;
    gc.gridx = 0;
    gc.gridy = 3;
    gc.anchor = GridBagConstraints.LINE_END;
    gc.insets = new Insets(0, 0, 0, 20);

    add(correctLabel, gc);

    gc.weighty = 0.1;
    gc.weightx = 1;
    gc.gridx = 1;
    gc.gridy = 3;
    gc.anchor = GridBagConstraints.LINE_START;
    gc.insets = new Insets(0, 0, 0, 0);

    add(incorrectLabel, gc);
}

public void setTestListener(TestListener listener) {
    this.listener = listener;
}

public int getCorrectCount() {
    return correctCount;
}

public int getIncorrectCount() {
    return incorrectCount;
}

public void setCorrectCount(int correctCount) {
    this.correctCount = correctCount;
}

public void setIncorrectCount(int incorrectCount) {
    this.incorrectCount = incorrectCount;
}

public void setCorrectLabel(String text) {
    correctLabel.setText(text);
}

public void setIncorrectLabel(String text) {
    incorrectLabel.setText(text);
}

@Override
public void keyPressed(KeyEvent keySource) {

    int key = keySource.getKeyCode();

    if(key == KeyEvent.VK_ENTER) {
        if (defField.getText().equals("")) {

        } else {

            String root = word.getRoot();
            String def = word.getDef();
            boolean status;
            if (defField.getText().equals(word.getDef())) {
                correctCount++;
                correctLabel.setText("Correct: " + correctCount);
                status = true;
            } else {
                incorrectCount++;
                incorrectLabel.setText("Incorrect: " + incorrectCount);
                status = false;
            }

            defField.setText("");
            word.setRootAndDef();
            rootField.setText(word.getRoot());
            TestEvent event = new TestEvent(ok, root, def, status);
            listener.dataSubmitted(event);

        }
    }

}

@Override
public void keyReleased(KeyEvent keySource) {

    int key = keySource.getKeyCode();

    if(key == KeyEvent.VK_ENTER) {
        if (defField.getText().equals("")) {

        } else {

            String root = word.getRoot();
            String def = word.getDef();
            boolean status;
            if (defField.getText().equals(word.getDef())) {
                correctCount++;
                correctLabel.setText("Correct: " + correctCount);
                status = true;
            } else {
                incorrectCount++;
                incorrectLabel.setText("Incorrect: " + incorrectCount);
                status = false;
            }

            defField.setText("");
            word.setRootAndDef();
            rootField.setText(word.getRoot());
            TestEvent event = new TestEvent(ok, root, def, status);
            listener.dataSubmitted(event);

        }
    }

}

@Override
public void keyTyped(KeyEvent keySource) {

    int key = keySource.getKeyCode();

    if(key == KeyEvent.VK_ENTER) {
        if (defField.getText().equals("")) {

        } else {

            String root = word.getRoot();
            String def = word.getDef();
            boolean status;
            if (defField.getText().equals(word.getDef())) {
                correctCount++;
                correctLabel.setText("Correct: " + correctCount);
                status = true;
            } else {
                incorrectCount++;
                incorrectLabel.setText("Incorrect: " + incorrectCount);
                status = false;
            }

            defField.setText("");
            word.setRootAndDef();
            rootField.setText(word.getRoot());
            TestEvent event = new TestEvent(ok, root, def, status);
            listener.dataSubmitted(event);

        }
    }

}

}

1 个答案:

答案 0 :(得分:2)

  

我有一个实现KeyListener的小类,但即使我实现了所有方法,包括keyPressed(),并且给了它们所有代码来运行,当我按下enter(return)键时没有任何反应。请帮我解决这个问题,这里是我实现KeyListener的类(实现的方法在底部):

简短的回答,不要。如果您认为自己需要setTimeout(function () { $('.google-visualization-table').css("z-index", "1"); }, 500); ,那么您可能不会感到兴奋。 KeyListener是一个很少需要使用的低级API,因为通常有更好的方法来实现相同的功能。

在您的情况下,KeyListenerJTextField都通过JButton界面支持事件通知。在ActionListener的情况下,当用户按下"动作时,这将通知您。当字段被聚焦时键,而在JTextField的情况下,当用户按下"动作"时,按钮助记键组合或用鼠标按下按钮。

JButton API旨在消除检测此功能所涉及的复杂性,并为您提供一个简单的回调,您可以通过该回调执行所需的操作。让我们面对现实,我们并不关心"如何"组件被激活,只是它

相反,摆脱ActionListener并简单地使用类似......

的东西
KeyListener

这样,用户不需要按下按钮,但可以按下"动作"该字段的密钥和相同的功能将被执行。

有关详细信息,请参阅How to Use Text FieldsHow to Use Buttons, Check Boxes, and Radio ButtonsHow to Write an Action Listeners