在for语句中使用Thread.sleep(1000)不可以么?
如下代码段:
try{
for(int i=0;i<chatrecordString.size();i++)
{
displayArea.append((String)chatrecordString.get(i));
Thread.sleep(1000);
}
}catch(InterruptedException e) {}
我想动态的每显示一行停顿一会(用sleep),但是显示的过程却是停顿之后把所有行全显示出来了。
请问如何达到每显示一行停顿一会呢?
这里没有声明线程
问题点数:20、回复次数:7Top
1 楼shenpipi(皮皮)回复于 2005-08-03 22:13:08 得分 0
for中当然可以有sleep了.Top
2 楼interhanchi(on the Java Road)回复于 2005-08-03 22:25:53 得分 0
你本身main函数就是一个线程!Top
3 楼litengda(暗夜天使1)回复于 2005-08-03 22:30:30 得分 0
应该可以的!!你在检查一下!Top
4 楼OO00(四个圈)回复于 2005-08-04 00:15:16 得分 0
1.把显示一行用的函数或方法写到for里面了吗?
是isplayArea.append((String)chatrecordString.get(i));???
2.用this调用线程Top
5 楼mooninsun()回复于 2005-08-04 00:35:20 得分 0
你是不是其他地方写错了
我写了一个类似的程序,没有问题
public class forSleep {
static int i = 0;
public static void main(String[] args) {
try {
for (i = 0; i < 1000; i++) {
System.out.println(i);
Thread.sleep(1000);
}
}catch(InterruptedException e) {
System.err.println(e.toString());
}
}
}Top
6 楼mooninsun()回复于 2005-08-04 00:56:29 得分 20
public class forSleepA
extends JFrame {
private JTextArea t = new JTextArea(10, 15);
private ArrayList chatrecordString = new ArrayList();
public forSleepA() throws HeadlessException {
super("Test");
this.setSize(300, 200);
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(t);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for (int i = 0; i < 10; i++) {
chatrecordString.add("String_" + i);
}
try {
for (int i = 0; i < chatrecordString.size(); i++) {
t.append( (String) chatrecordString.get(i) + "\n");
Thread.sleep(1000);
}
}
catch (InterruptedException e) {
System.err.println(e.toString());
}
}
public static void main(String[] args) throws HeadlessException {
forSleepA forSleepA1 = new forSleepA();
}
}
写了个和你一样的程序也没问题
个人感觉你的问题是其他问题引起的Top
7 楼liuchang2859(liuchang)回复于 2005-08-13 02:26:43 得分 0
public void autoPlay()
{
chatrecordtoString();
sortcharrecord();
displayArea.setText("");
try{
for(int i=0;i<chatrecordString.size();i++)
{
displayArea.append((String)chatrecordString.get(i));
System.out.println(i);
Thread.sleep(1000);
}
}catch(InterruptedException e) {}
}
谢谢,谢谢!
真怪了,我把System.out.println(i);放进去,结果系统输出正常,displayArea还是等到0,1,2全输出完之后在一次性输出。实在不知道怎么回事?Top




