如何更改 Java 面板上的背景颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4219919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the background color on a Java panel?
提问by razshan
Right now, the background I get is a grey. I want to change it to black. I tried doing something like setBackground(color.BLACK); but it didnt work. Any suggestions?
现在,我得到的背景是灰色的。我想把它改成黑色。我尝试做类似 setBackground(color.BLACK); 的事情。但它没有用。有什么建议?
public test()
{
setTitle("Adjustment Form");
setSize(670,450);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridLayout(4,6,2,2));
setVisible(true);
}
采纳答案by user489041
You could call:
你可以打电话:
getContentPane().setBackground(Color.black);
Or add a JPanel to the JFrame your using. Then add your components to the JPanel. This will allow you to call
或者将 JPanel 添加到您使用的 JFrame。然后将您的组件添加到 JPanel。这将允许您调用
setBackground(Color.black);
on the JPanel to set the background color.
在 JPanel 上设置背景颜色。
回答by Michael Mrozek
setBackground()
is the right method to use. Did you repaint after you changed it? If you change it before you make the panel (or its containing frame) visible it should work
setBackground()
是正确的使用方法。换了之后有没有重新粉刷?如果在使面板(或其包含框架)可见之前更改它,它应该可以工作
回答by BigMac66
I am assuming that we are dealing with a JFrame? The visible portion in the content pane - you have to use jframe.getContentPane().setBackground(...);
我假设我们正在处理 JFrame?内容窗格中的可见部分 - 您必须使用 jframe.getContentPane().setBackground(...);
回答by yormen
I think what he is trying to say is to use the
getContentPane().setBackground(Color.the_Color_you_want_here)
我认为他想说的是使用
getContentPane().setBackground(Color.the_Color_you_want_here)
but if u want to set the color to any other then the JFrame, you use the object.setBackground(Color.the_Color_you_want_here)
但是如果你想将颜色设置为 JFrame 之外的任何其他颜色,你可以使用 object.setBackground(Color.the_Color_you_want_here)
Eg:
例如:
jPanel.setbackground(Color.BLUE)