我有以下代码
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 -> 按回车键时输出如下
Key Pressed
Key keyTyped
Action performed
jdk1.7.0_12 -> 按回车键时输出如下
Key Pressed
那么 java 7 有什么问题呢?
当我输入任何数字并清除控制台时。然后按 Enter 键焦点更改为下一个组件,但在 java 7 中从未触发过 actionperformed。我该如何解决?我也检查了 java 7 update 25。我得到了同样的结果。有人可以帮忙吗?