java - JFrame 背景颜色未正确更改

标签 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

最佳答案

嗯..有趣的一个.. 尝试给它一些 sleep ,给一些延迟时间..

这取决于您的计算机规范。尽管.. 就我而言,我需要为每个帧创建提供大约 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);
            }
        });

    }
}

关于java - JFrame 背景颜色未正确更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23601163/

相关文章:

java - java的replaceAll()方法中的正则表达式不匹配

java - 当我使用 java.awt.Canvas 时,JSplitPane 不会调整大小?

java - 为什么删除功能在我的 jsp 文件中不起作用?

java - 添加到 ArrayList 时如何避免 "incompatible parameter types in lambda expression"?

java - .drawLine() 问题和缓冲图像

java - 我生疏了,不知道如何在 Java swing 中正确处理异常

java - 为什么我的自定义按钮不起作用?

java - 如何在 Java 中创建屏幕键盘?

java - JFrame.setExtendedState 实际上并没有最大化

java - 如何比较 Jtextfield 中的 2 个整数