我想弄清楚为什么文本字段没有更新。我知道使用SwingWorker可能会解决这个问题,但我不明白为什么它不起作用。JTextField不使用Thread.sleep更新()
public class waitExample {
private JFrame frame;
private JTextField txtLeadingText;
private String one = "update string 1";
private String two = "update string 2";
private String three = "update string 3";
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
waitExample window = new waitExample();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public waitExample() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
txtLeadingText = new JTextField();
txtLeadingText.setHorizontalAlignment(SwingConstants.CENTER);
txtLeadingText.setText("leading text");
frame.getContentPane().add(txtLeadingText, BorderLayout.SOUTH);
txtLeadingText.setColumns(10);
JButton btnClickMeTo = new JButton("CLICK ME TO UPDATE TEXT");
btnClickMeTo.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
try {
updateOne();
Thread.sleep(1000);
updateTwo();
Thread.sleep(1000);
updateThree();
Thread.sleep(1000);
updateLast();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
frame.getContentPane().add(btnClickMeTo, BorderLayout.CENTER);
}
private void updateOne() {
txtLeadingText.setText(one);
}
private void updateTwo() {
txtLeadingText.setText(two);
}
private void updateThree() {
txtLeadingText.setText(three);
}
private void updateLast() {
txtLeadingText.setText("default text");
}
}
据我所知,默认的线程将阻止任何GUI更新。这应该不重要,因为我在Thread.sleep之前设置了textField。 为什么不更新文本字段?不应该设置文本,然后线程等待?
编辑:根据答案,上述代码已被更新。
为了进一步解释:当您调用类似'txtLeadingText.setText(one)'的函数时,GUI上显示的文本不会立即更新。相反,文本字段的内部变量将会更改,并且它还会通知Swing队列它需要重新绘制。因为这经历了事件队列,所以它只能在函数返回后通过'repaint()'*运行。 –
@AlexGittemeier根据答案和您的评论,我已经更改了代码(请参阅帖子)。更新后的代码反映了我从您的意见中了解的内容,但它仍不能解决错误。如果'重绘();'被称为_after_从方法返回,不应该一切正确更新? – Aaron
@Aaron这仍然是在同一个线程中执行。现在它是这样的:'enter mouseClicked()' - >'enter updateOne()' - >'exit updateOne()' - >'enter ...' - >'exit mouseClicked()' - > ...- >'execute repaint()' – Xeon