我想在每秒后重新绘制组件,但它不起作用.我在想的是:
try{
while(true){
Thread.currentThread().sleep(1000);
gc.cb.next();
gc.repaint();
}
}
catch(Exception ie){
}
Run Code Online (Sandbox Code Playgroud)
Ada*_*ski 10
我建议使用javax.swing.Timerfor这个问题,它会定期触发ActionEventEvent Dispatch线程(注意你应该只调用重绘和/或操作来自这个线程的Swing组件).然后,您可以定义一个ActionListener拦截事件并在此时重新绘制组件.
例
JComponent myComponent = ...
int delay = 1000; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
myComponent.repaint();
}
};
new Timer(delay, taskPerformer).start();
Run Code Online (Sandbox Code Playgroud)
另请注意,这SwingWorker可能不合适,因为它通常用于具有已定义的开始和结束的后台任务,而不是定期任务.