如何检查KeyListener输入?

时间:2010-12-02 16:45:11

标签: java keylistener

在java.awt.event.KeyListener中,如何检查用户是否输入“+”或“ - ”?

1 个答案:

答案 0 :(得分:1)

以下是如何执行此操作的代码示例:

    TextField field = new TextField();
    field.addKeyListener(new KeyListener() {
        public void keyTyped(KeyEvent e) {
            final char aChar = e.getKeyChar();
            if (aChar == '+') {
                System.out.println("+ was typed");
            }
            if (aChar == '-') {
                System.out.println("- was typed");
            }
        }

        public void keyPressed(KeyEvent e) {
        }

        public void keyReleased(KeyEvent e) {
        }
    });