简体   繁体   English

更改 JFrame 的背景颜色

[英]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 的颜色,但我似乎无法弄清楚......

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);使用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 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如果您不想取消装饰 JFrame

You can use a JPanel instead it.您可以使用 JPanel 代替它。

Or use Answer No 1或使用答案 1

But you can only use this fr.getContentPane().setBackground(Color.BLACK);但是你只能使用这个fr.getContentPane().setBackground(Color.BLACK); code in main method.主要方法中的代码。

So Use this this answer's code.所以使用这个答案的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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