2013-11-25 41 views
0
for (do something in) { 
TextArea.append("Text here"); 
//break for one second 
} 

我的问题是我该如何中断一秒钟,并直接显示在textarea上,而不是在同一时间?Java TextArea更新

回答

0

暂停执行一秒可以用

Thread.sleep(1000); 

可以做到,但你应该通过捕捉可能InterruptedException

try 
{ 
    Thread.sleep(1000); 
} 
catch(InterruptedException e) 
{ 
System.out.println(e.getMessage()); 
} 

你也应该更改GUI组件

EventQueue.invokeLater(new Runnable(){ 
    public void run() 
    { 
    //make gui change here 
    } 
}); 

这从AWT GUI事件调度线程(EDT)更新它。

+0

我知道,你可以使用,但随后仍然显示这一切在同一时间 – Pay4yourlife

+0

然后我假设你有文字的'TextArea.append一大块( “文字在这里”);'。在这种情况下,您需要手动将文本分解为块,并在每个循环中输出一个块。 –

+0

修好了,谢谢! – Pay4yourlife

0

对于突破仅一个第二,你必须使用睡眠(1000)Thread类,并实现多线程,你的方法必须重写run方法和扩展Thread类

0

它可能如果可以一次显示所有TextArea.append调用不在swing线程中发生。下面的块请求摆动线程执行可运行的代码,阻塞当前线程直到完成。然后,我们做我们的第一秒的睡眠

final JTextArea someArea = new JTextArea(); 

    try { 
     SwingUtilities.invokeAndWait(new Runnable() { 

      public void run() { 
       someArea.append("Some text"); 
      } 
     }); 
    } catch (InterruptedException ex) { 
     Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (InvocationTargetException ex) { 
     Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    try { 
     Thread.sleep(1000); 
    } catch (InterruptedException ex) { 
     Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex); 
    }