如下代码段:try{
for(int i=0;i<chatrecordString.size();i++)
{
displayArea.append((String)chatrecordString.get(i));
Thread.sleep(1000);
}
}catch(InterruptedException e) {}我想动态的每显示一行停顿一会(用sleep),但是显示的过程却是停顿之后把所有行全显示出来了。
请问如何达到每显示一行停顿一会呢?这里没有声明线程

解决方案 »

  1.   

    你本身main函数就是一个线程!
      

  2.   

    1.把显示一行用的函数或方法写到for里面了吗?
       是isplayArea.append((String)chatrecordString.get(i));???2.用this调用线程
      

  3.   

    你是不是其他地方写错了
    我写了一个类似的程序,没有问题
    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());
        }
      }
    }
      

  4.   

    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();
      }}
    写了个和你一样的程序也没问题
    个人感觉你的问题是其他问题引起的
      

  5.   

    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全输出完之后在一次性输出。实在不知道怎么回事?