我遇到的问题是,当文本字段获得焦点时,键击会丢失而不是排队并处理 . 这曾经在Java 6中正常工作,但在Java 7中不再正常工作 .
这是场景:
-
用户打开一个对话框,该对话框具有焦点的文本字段
-
在对话框出现之前,用户开始输入内容
-
对话框启动后,文本字段具有焦点:
Java 6:用户之前键入的任何内容都显示在文本字段中
Java 7:文本字段中没有任何内容
任何想法为什么会这样?
使用以下示例代码:
单击该按钮时,另一个对话框将需要4秒钟才能加载 . 在那段时间,开始按键盘上的一些字母数字键 . 第二个对话框出现后,您将在文本字段中看到您的按键 . 这适用于Java6,但不适用于Java7 .
public class UITest{
public static void main(String[] args)
{
final UITest example = new UITest();
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
example.createMainDialog();
}
});
}
public void createMainDialog()
{
JFrame frame = new JFrame("Main Dialog");
frame.setLayout(new FlowLayout());
JButton button = new JButton("Click Me");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
createOtherDialog();
}
});
frame.add(button);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public void createOtherDialog()
{
//I know this is bad to do in the EDT, but just trying to simulate a busy EDT
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
JFrame frame = new JFrame("Other Dialog");
frame.setLayout(new FlowLayout());
JTextField field = new JTextField(25);
frame.add(field);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
//Text field has focus
field.requestFocus();
}
}