<分区>
Possible Duplicate:
GUI threading in java
我一直在尝试制作一个基于文本的游戏,并且到目前为止,除了使用 Thread.sleep() 和 wait() 时出现的这个奇怪错误外 此代码应逐个字符地将一条消息打印到名为控制台的 JTextArea,每条消息之间都有延迟。
这里是wait()的代码
int i=0;
synchronized(mon) {
while(i<msg.length())
{
console.setText(console.getText()+ msg.charAt(i));
i++;
try {
mon.wait(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
这是带有 sleep() 的代码:
int i=0;
while(i<msg.length())
{
console.setText(console.getText()+ msg.charAt(i));
i++;
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
但是,当它到达此代码时,程序会等待 msg.length*500ms,然后立即打印整个 msg!帮助!