在一个面板上交替显示红色的圆和绿色的圆

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class ShiYan04 {
public static void main(String args[])
{
Ckou ck=new Ckou();
}

}

class Ckou extends JFrame
{
public Ckou()
{
JFrame jframe=new JFrame("变色圆");
Container container=jframe.getContentPane();
jframe.setBackground(Color.BLUE);
jframe.setSize(600,400);
jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jframe.setVisible(true);

    Drawt dt=new Drawt();
    container.add(dt);

    JButton button=new JButton("开始");
    button.setSize(30, 30);
    container.add(button);
    button.addActionListener(new ActionListener()
            {

            public void actionPerformed(ActionEvent e)
            {
                  T2 t2=new T2(dt);
                  Thread tt2=new Thread(t2);
                  T1 t1=new T1(dt,t2);
                  Thread tt1=new Thread(t1);
                  tt1.start();
                  tt2.start();
                  try
                  {
                         Thread.sleep(100000);
                  }
                  catch(Exception e3)
                  {}

            }
            }
            );

}
class T1 implements Runnable
{
    Drawt t;
    T2 a;
    public T1(Drawt t,T2 a)
    {
        this.t=t;
        this.a=a;
    }
    public void run()
    {
        while(true)
        {
            System.out.println("现在显示绿色");
            a.stop=true;
            t.i=1;
            t.repaint();
            try
            {
                Thread.sleep(3000);
            }
            catch(Exception e1)
            {}
            a.stop=false;
            try
            {
                Thread.sleep(3000);
            }
            catch(Exception e1)
            {}

        }
    }
}
class T2 implements Runnable
{
    volatile boolean stop = true;
    Drawt t;
    public T2(Drawt t)
    {
        this.t=t;
    }
    public void run()
    {
        while(!stop)
        {
            System.out.println("现在显示红色");
            t.i=2;
            try
            {
                Thread.sleep(3000);
            }
            catch(Exception e2)
            {}
        }

    }

}
class Drawt extends JPanel
{

    int i=0;
     public Drawt()
     {}

     public void paint(Graphics g)
     {
         if(i==1)
         { 
             g.setColor(Color.green);
             g.drawOval(100,100,70, 70);
         }
         else if(i==2)
         {
             g.setColor(Color.red);
             g.drawOval(150,150,70, 70); 
         }
     }
}

}
首先他一直输出的是绿色的圆,
然后就是由于加上主线程main的睡眠,我的圆显示不出来

你的代码存在的问题:
1)你的T2线程的while循环第一次执行就跳出了,导致红色线程一下就结束了。应该也是跟T1一样用while(true)内部操作时根据i的值绘图。
2)Button的宽度太小了,导致看不到文本值。
3)绘制圆形是两个不一样的圆形,所以看不到交替效果。
4)main线程sleep时间太长了,可以改小一点。
修正你的代码如下:

 import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class ShiYan04 {
    public static void main(String args[]) {
        Ckou ck = new Ckou();
    }
}

class Ckou extends JFrame {
    public Ckou() {
        JFrame jframe = new JFrame("变色圆");
        Container container = jframe.getContentPane();
        jframe.setBackground(Color.BLUE);
        jframe.setSize(600, 400);
        jframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jframe.setVisible(true);
        Drawt dt = new Drawt();
        container.add(dt);

        JButton button = new JButton("开始");
        button.setSize(100, 30);
        container.add(button);
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                T2 t2 = new T2(dt);
                Thread tt2 = new Thread(t2);
                T1 t1 = new T1(dt, t2);
                Thread tt1 = new Thread(t1);
                tt1.start();
                tt2.start();
                try {
                    Thread.sleep(100000);
                } catch (Exception e3) {
                }

            }
        });

    }

    class T1 implements Runnable {
        Drawt t;
        T2 a;

        public T1(Drawt t, T2 a) {
            this.t = t;
            this.a = a;
        }

        public void run() {
            while (true) {
                System.out.println("现在显示绿色");
                a.stop = true;
                t.i = 1;
                t.repaint();
                try {
                    Thread.sleep(3000);
                } catch (Exception e1) {
                }
                a.stop = false;
                try {
                    Thread.sleep(3000);
                } catch (Exception e1) {
                }

            }
        }
    }

    class T2 implements Runnable {
        volatile boolean stop = true;
        Drawt t;

        public T2(Drawt t) {
            this.t = t;
        }

        public void run() {

            while (true) {
                System.out.println("stop :"+stop);  
                System.out.println("现在显示红色");
                t.i = 2;
                try {
                    Thread.sleep(3000);
                } catch (Exception e2) {
                }
            }

        }

    }

    class Drawt extends JPanel {

        int i = 0;

        public Drawt() {
        }

        public void paint(Graphics g) {
            System.out.println("paint i:"+i);
            if (i == 1) {
                g.setColor(Color.green);
                g.drawOval(150, 150, 70, 70);
            } else if (i == 2) {
                g.setColor(Color.red);
                g.drawOval(150, 150, 70, 70);
            }else{
                g.setColor(Color.ORANGE);
                g.drawOval(150, 150, 70, 70);
            }
        }
    }
}

测试OK。可以优化的是paint方法的g.drawOval代码放在外面,只在if分支中设置颜色,添加一个初始默认颜色。

        public void paint(Graphics g) {
            System.out.println("paint i:"+i);
            if (i == 1) {
                g.setColor(Color.green);
            } else if (i == 2) {
                g.setColor(Color.red);
            }else{
                g.setColor(Color.ORANGE);
            }
            g.drawOval(150, 150, 70, 70);
        }