[英]JFrame back ground color doesn't change correctly
我正在創建一個應用程序,該應用程序使用不同的背景顏色創建大量的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);
}
}
嗯..有趣的一個..試着給它一點睡眠,給一些延遲時間..
這將取決於您的計算機規格。 雖然..在我的情況下,我需要給每幀創建大約100ms的延遲..它仍然可以很好地工作到444幀,然后我停止了它。
如果我將其減少到50ms延遲,我在大約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);
}
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.