Use frame.getContentPane().setBackground(Color.BLACK);
to set the color.
Change background color of a JFrame
Domanda
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);
Soluzione
Altri suggerimenti
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.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow