Java 1.7问题,按键输入键的工作方式不同

时间:2013-06-23 20:24:41

标签: java java-7

我有以下代码

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TesttxtF {

    /**
     * @param args
     */
    public static void main(String[] args) {
        TextField txt1 = new TextField();
        TextField txt2 = new TextField();

        DefaultFocusManager manager = new DefaultFocusManager() {
            @Override
            public void processKeyEvent(Component focusedComponent, KeyEvent e) {
                switch (e.getKeyCode()) {
                case KeyEvent.VK_ENTER:
                    if (e.getID() == KeyEvent.KEY_PRESSED)
                        super.focusNextComponent(focusedComponent);
                    else
                        super.processKeyEvent(focusedComponent, e);
                    break;

                default:
                    super.processKeyEvent(focusedComponent, e);
                    break;
                }

            }
        };
        FocusManager.setCurrentManager(manager);

        JPanel panel = new JPanel();
        panel.add(txt1);
        panel.add(txt2);

        txt1.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {
                System.out.println("Key keyTyped");
            }

            @Override
            public void keyReleased(KeyEvent e) {
                System.out.println("Key keyReleased");
            }

            @Override
            public void keyPressed(KeyEvent e) {
                System.out.println("Key Pressed");
            }
        });

        txt1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Action performed");
            }
        });

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(panel, null);
        frame.setSize(100, 100);
        frame.setVisible(true);
    }

}

使用

运行时

jdk1.6.0_18 - >按Enter键时输出如下

Key Pressed
Key keyTyped
Action performed

jdk1.7.0_12 - >按Enter键时输出如下

Key Pressed

那么java 7有什么问题呢?

当我键入任何数字并清除控制台时。然后按Enter键焦点更改为下一个组件,但执行操作从未在java 7中触发。我如何解决它?我也检查过java 7 update 25。我得到了同样的结果。有人可以帮忙吗?

0 个答案:

没有答案