JFrame背景颜色未正确更改

时间:2014-05-12 04:41:08

标签: java swing jframe

我正在创建一个创建大量具有不同背景颜色的JFrame的应用程序。起初它们是正确的颜色(黑色和红色),但所有新的颜色都保持白色。

import java.awt.Color;
import java.util.Random;

import javax.swing.JFrame;

public class JFrameCrash{

    private Random r;
    private int screenHeight;
    private int screenWidth;

    private static final int FRAME_HEIGHT = 100;
    private static final int FRAME_WIDTH = 200;
    //private static final Color[] COLORS = {Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW, Color.BLACK, Color.WHITE, Color.GRAY, Color.CYAN, Color.PINK, Color.MAGENTA, Color.ORANGE};
    private static final Color[] COLORS = {Color.RED, Color.BLACK};

    public static void main(String[] args){
        new JFrameCrash();
    }

    public JFrameCrash(){
        screenHeight = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height;
        screenWidth = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width;
        r = new Random();
        loop();
    }

    public JFrameCrash(int height, int width, Random rand){
        this.screenHeight = height;
        this.screenWidth = width;
        r = rand;
        run();
    }

    private void loop(){
        while (true){
            new JFrameCrash(screenHeight, screenWidth, r);
        }
    }

    private void constructFrame(){
        JFrame frame = new JFrame();
        frame.setTitle("");
        frame.setLocation(r.nextInt(screenWidth), r.nextInt(screenHeight));
        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        frame.getContentPane().setBackground(COLORS[r.nextInt(COLORS.length)]);
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.setResizable(false);  
        frame.setVisible(true);
    }
}

enter image description here

1 个答案:

答案 0 :(得分:1)

嗯...有趣的一个...... 试着给它一些睡眠,给一些延迟时间..

这取决于您的计算机规格。虽然.. 在我的情况下,我需要每帧创建大约100毫秒延迟.. 并且它仍然可以很好地工作到444帧,然后我就停止它..

如果我把它减少到50毫秒延迟,我在200左右的创造时获得了与你相同的体验..

有一个有趣的编程〜

import java.awt.Color;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class JFrameCrash {

    private Random r;
    private int screenHeight;
    private int screenWidth;

    private static final int FRAME_HEIGHT = 100;
    private static final int FRAME_WIDTH = 200;
    //private static final Color[] COLORS = {Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW, Color.BLACK, Color.WHITE, Color.GRAY, Color.CYAN, Color.PINK, Color.MAGENTA, Color.ORANGE};
    private static final Color[] COLORS = {Color.RED, Color.BLACK};

    public static void main(String[] args) {
        new JFrameCrash();
    }

    public JFrameCrash() {
        screenHeight = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height;
        screenWidth = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width;
        r = new Random();
        loop();
    }

    public JFrameCrash(int height, int width, Random rand) {
        this.screenHeight = height;
        this.screenWidth = width;
        r = rand;

        constructFrame();
        //run();
    }

    private void loop() {
        int i = 0;
        while (true) {
            new JFrameCrash(screenHeight, screenWidth, r);
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                Logger.getLogger(JFrameCrash.class.getName()).log(Level.SEVERE, null, ex);
            }
            i++;
            System.out.println(i);
        }
    }

    private void constructFrame() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setTitle("");
                frame.setLocation(r.nextInt(screenWidth), r.nextInt(screenHeight));
                frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
                frame.getContentPane().setBackground(COLORS[r.nextInt(COLORS.length)]);
                frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                frame.setResizable(false);
                frame.setVisible(true);
            }
        });

    }
}