每秒钟执行并显示图形

每秒钟执行并显示图形

问题描述:

我正在使用生物体的jLabels进行生物体生长模拟。然而,当我实现一个for循环和一个计时器来试图显示它移动时,它会冻结,然后显示标签的最终位置而不是显示它移动。有谁能向我解释为什么会发生这种情况吗?每秒钟执行并显示图形

public class TestView extends FrameView { 
    public TestView(SingleFrameApplication app) { 
     super(app); 
     initComponents(); 
     picture = new JLabel(); 
     picture.setIcon(new ImageIcon(System.getProperty("user.dir") + 
      File.separator + "mouse.gif")); 
     picture.setBounds(0, 0, 100, 100); 
     mainPanel.add(picture); 

     for (int x = 0; x < 20; x++) { 
     move(); 
     wait(50); 
     } 
    } 

    public static void wait(int n) { 
     long t0, t1; 
     t0 = System.currentTimeMillis(); 
     do { 
      t1 = System.currentTimeMillis(); 
     } while (t1 - t0 < n); 
    } 

    public static void move() { 
     picture.setBounds(picture.getX() + 5, picture.getY(), 100, 100); 
    } 
+5

不要阻塞EDT(Event Dispatch Thread) - 当发生这种情况时GUI将“冻结”。除了调用'Thread.sleep(n)'或者进入一个紧密的'什么都不做'循环之外,还可以实现一个Swing'Timer'来重复执行任务或者一个SwingWorker来执行长时间运行的任务。有关更多详细信息,请参见[Swing中的并发](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/)。 – 2013-02-12 02:26:44

你可能会喜欢的diffusion limited aggregationsimulations这个例子。