A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

import java.awt.*;
import java.awt.event.*;

public class Tank extends Frame {

        int x = 50, y = 50;
       
        public void paint(Graphics g) {
                Color c = g.getColor();
                g.setColor(Color.RED);
                g.fillOval(x,y,30,30);
                g.setColor(c);
               
                y += 5;
        }

        public void launchFrame() {
                setSize(800, 600);
                setLocation(200, 200);
                setTitle("TankWar");
                this.addWindowListener(new WindowAdapter() {
                        public void windowClosing(WindowEvent e) {
                                System.exit(0);
                        }
                });
                setResizable(false);
                setBackground(new Color(100,255,100));
                setVisible(true);
               
                new Thread(new PaintThread()).start();
        }

        public static void main(String[] args) {
                Tank tc = new Tank();
                tc.launchFrame();
        }
       
        private class PaintThread implements Runnable {

                public void run() {
                        try {
                                while(true) {
                                        repaint();
                                        Thread.sleep(100);
                                }
                        } catch (InterruptedException e) {
                                        e.printStackTrace();
                               
                        }
                }
               
        }

}
我在eclipse上面运行为什么那个红色原点一直闪烁。。 闪烁的很厉害 怎么样才能不让他闪烁呢?

1 个回复

倒序浏览
Thread.sleep(100);

是线程的原因、、你改成 Thread.sleep(1000);就不会闪了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马