非常多的标题。代码应该绘制一个盒子,等待1秒钟,然后在另一个位置绘制一个新盒子并重新绘制。相反,它会等待1秒钟,然后绘制两个盒子。感谢您的帮助和抱歉,如果我搞乱格式化。Thread.sleep()延迟整个程序,而不仅仅是它之后
import javax.swing.*;
import java.awt.*;
public class GameRunner extends JPanel{
@Override
public void paintComponent (Graphics g){
int x = 0;
boolean directionRight = true;
g.setColor(Color.blue);
g.fillRect(300,400,100,100);
repaint();
try{
Thread.sleep(1000);
}
catch (Exception ex){}
g.fillRect(600,400,100,100);
repaint();
}
public static void main (String[] args){
JFrame frame = new JFrame("Submarine");
GameRunner gameRunner = new GameRunner();
frame.add(gameRunner);
frame.setSize(1200,700);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
你需要在你的'main'睡觉 - 调用方法两次 –
欢迎来到“妈妈,我冻结事件调度线程”的美妙世界。先看看[AWT和Swing中的绘画](http://www.oracle.com/technetwork/java/painting-140037.html)和[Swing中的并发](http://docs.oracle.com/javase/tutorial/uiswing/concurrency /)来了解Swing的绘画过程如何工作。然后看看[如何使用Swing Timers](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html)获取可能的解决方案 – MadProgrammer
我在哪里可以完全明了? –