[英]JFrame back ground color doesn't change correctly
I am creating an application that creates a ton of JFrames with different background colors. 我正在创建一个应用程序,该应用程序使用不同的背景颜色创建大量的JFrame。 At first they are the correct colors (Black and Red) but then all the new ones stay white. 首先,它们是正确的颜色(黑色和红色),但是所有新颜色都保持白色。
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);
}
}
hmm.. interesting one.. try to give it some sleep, give some delay time.. 嗯..有趣的一个..试着给它一点睡眠,给一些延迟时间..
it will depend on your computer spec. 这将取决于您的计算机规格。 though.. in my case, I need to give around 100ms delay per frame creation.. and it can still works well up to 444 frames, then I just stopped it.. 虽然..在我的情况下,我需要给每帧创建大约100ms的延迟..它仍然可以很好地工作到444帧,然后我停止了它。
if I reduce it to 50ms delay, I got the same experience as you at around 200-ish creation.. 如果我将其减少到50ms延迟,我在大约200倍的创作过程中会获得与您相同的体验。
have a fun programming~ 玩得开心〜
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);
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.