qq_32390687 2015-10-31 13:32 采纳率: 50%
浏览 1690

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

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条回答 默认 最新

  • 毕小宝 博客专家认证 2015-11-02 05:27
    关注

    你的代码存在的问题:
    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);
            }
    
    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波