package programming;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class GuiMultiThread extends JFrame {   public GuiMultiThread()
{
this.add(new MultiThreadPanel());
this.setSize(1000, 500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
   

public static void main(String[] args) {
        new GuiMultiThread();
        
        Thread thread=new Thread(new MultiThreadPanel());
        
        thread.start();
}}
class MultiThreadPanel extends JPanel implements Runnable
{  
 double x1,x2=0,y=0,t=0;

   public void paintComponent(Graphics g)
   {
  super.paintComponent(g); 
   y=9.8*t*t/2;
   x2=26*t;
   g.fillOval((int)x1,(int)y,40,40);
   g.fillOval((int)x2, (int)y, 40, 40);   }
   public void run()
   {
   try{
   while(true){  
      Thread.sleep(50);
      t+=0.05;
      this.repaint();
   }
   
   }catch(InterruptedException i)
   {i.printStackTrace();}
   
   }
  
   
}

解决方案 »

  1.   

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class GuiMultiThread extends JFrame {    public GuiMultiThread()
        {
            MultiThreadPanel display = new MultiThreadPanel();
            this.add(display);
            this.setSize(1000, 500);
            this.setVisible(true);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            new Thread(display).start();
        }    public static void main(String[] args) {
            new GuiMultiThread();
        }
        private static class MultiThreadPanel extends JPanel implements Runnable
        {
            double x1,x2=0,y=0,t=0;        @Override public void paintComponent(Graphics g)
            {
                super.paintComponent(g);
                y= 9.8*t*t/2;
                x2 = 26*t;
                g.fillOval((int)x1,(int)y,40,40);
                g.fillOval((int)x2, (int)y, 40, 40);
            }        @Override public void run()
            {
                try{
                    while(true){
                        Thread.sleep(50);
                        t += 0.05;
                        this.repaint();
                    }
                }catch(InterruptedException i)
                {
                    i.printStackTrace();
                }
            }
        }
    }
      

  2.   

    呵呵,非常感谢,调试通过,但为什么这么改下就可以了?为什么必须把
    Thread thread=new Thread(new MultiThreadPanel());
        
      thread.start();  这两句放在构造函数里面?
      

  3.   

    原因:你放到frame里的和启动线程的不是同一个面板。