简体   繁体   中英

Change background color of a JFrame

I am trying to change the color of a JFrame with no components inside it, but I can't seem to figure it out...

JFrame frame = new JFrame();
frame.setTitle("");
// Attempts to change the color
frame.setBackground(Color.BLACK);
frame.setForeground(Color.BLACK);
// Attempts to change the color
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setResizable(false);  
frame.setVisible(true);

Use frame.getContentPane().setBackground(Color.BLACK); to set the color.

JFrame frame = new JFrame();
frame.setTitle("");
// Attempts to change the color
frame.setBackground(Color.BLACK);
frame.setForeground(Color.BLACK);
// Attempts to change the color
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setResizable(false);  
frame.setVisible(true);

You must use the frame as a undecorated jframe The New Code Here

JFrame frame = new JFrame();
setUndecorated(true);
frame.setTitle("");
// Attempts to change the color
frame.setBackground(Color.BLACK);
frame.setForeground(Color.BLACK); // by this code you haven't give a black foreground so remove this line 
// Attempts to change the color
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setResizable(false);  
frame.setVisible(true);

If you not want to undecorate JFrame

You can use a JPanel instead it.

Or use Answer No 1

But you can only use this fr.getContentPane().setBackground(Color.BLACK); code in main method.

So Use this this answer's code.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM