我想延迟gui。我把2个for循环,然后重新绘制一个标签,但是那些2 for循环执行一个又一个标签重新绘制到最后。
我该怎么办?
for(int i=0; i<100000; i++){
System.out.println(i);
}
label.setBackground(Color.RED);
for(int i=0; i<100000; i++){
System.out.println(i);
}
label.setBackground(Color.green);
答案 0 :(得分:2)
您可能需要查看
它是在Java中使用计时器的链接,它有助于从程序中删除for循环。
您可以改用:
Timer t = new Timer(2000, YourActionListener);
t.start();
}//End of method
public void paintComponent()
{
super.paintComponent(g);
if(c%2==0)
{
label.setBackground(Color.RED);
}
else
{
label.setBackground(Color.GREEN);
}
c++;
}
...
public void actionPerformed(ActionEvent) // How your YourActionListener method looks like
{
repaint();
}
答案 1 :(得分:0)
有几种方法可以延迟Java程序。最简单的选择是
Thread.sleep(2000);
这只是让当前线程休眠2秒钟。根据您要完成的任务,您可能需要考虑一些更复杂的选项。
此选项会使您的程序对睡眠时间无响应。正如你有(我假设)摇摆应用程序,你可以使用Timer代替。