I am creating an application that creates a ton of JFrames with different background colors. 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..
if I reduce it to 50ms delay, I got the same experience as you at around 200-ish creation..
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);
}
});
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.