Java JFrame.setBackground() 不起作用——为什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2742270/
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
JFrame.setBackground() not working -- why?
提问by devoured elysium
JFrame mainFrame = new JFrame();
mainFrame.setSize(100, 100);
mainFrame.setBackground(Color.CYAN);
mainFrame.setVisible(true);
My intent is to create a window with a cyan background. What is wrong with this? My window doesn't get cyan, as I'd expect!
我的意图是创建一个带有青色背景的窗口。这有什么问题?正如我所料,我的窗户没有变成青色!
Also, could anyone point out why I seem to have all the colors in duplicate (there's a Color.CYAN and a Color.cyan). Is there any difference at all between the two? Maybe the older one was a constant from before there were enums in Java and the second one is from the Enum?
另外,谁能指出为什么我似乎有重复的所有颜色(有一个 Color.CYAN 和一个 Color.cyan)。两者之间有什么区别吗?也许较旧的一个是 Java 中枚举之前的常量,而第二个来自枚举?
Thanks
谢谢
采纳答案by coobird
Why is the windows not cyan as expected?
为什么窗口不是预期的青色?
The issue here is that the area where the contents of the JFrame
is being displayed is actually the "content pane", and not contents of the JFrame
itself.
这里的问题是JFrame
显示内容的区域实际上是“内容窗格”,而不是其JFrame
本身的内容。
Therefore, the following line:
因此,以下行:
mainFrame.setBackground(Color.CYAN);
Is changing the color of the JFrame
, but that is actually not the part which is immediately visible when the JFrame
is displayed.
正在更改 的颜色JFrame
,但这实际上不是显示 时立即可见的部分JFrame
。
What is needed is to change the color of what is called the "content pane* (please refer to How to Use Root Panesfor an illustration), by changing the above line to the following:
需要的是通过将上面的行更改为以下内容来更改所谓的“内容窗格*”的颜色(请参阅如何使用根窗格以获得说明):
mainFrame.getContentPane().setBackground(Color.CYAN);
Using Frames in Swing could be surprisingly unintuitive at the beginning, so I would strongly recommend taking a look at the resources I've listed at the bottom of this answer.
一开始在 Swing 中使用 Frames 可能会令人惊讶地不直观,因此我强烈建议您查看我在此答案底部列出的资源。
Is there a difference between Color.CYAN
and Color.cyan
?
有没有之间的差异Color.CYAN
和Color.cyan
?
No, there is no difference between the two -- they are both constants in the Color
class which are Color
objects themselves. The only difference is in the names of the constants.
不,两者之间没有区别——它们都是Color
类中的常量,它们Color
本身就是对象。唯一的区别在于常量的名称。
The constants with lowercase names were introduced when the Color
class was introduced (which appears to be JDK 1.0, as there is no "Since" notation in the Java API Specification for the Color
class), and the uppercase names were added later on in JDK 1.4.
在引入Color
类时引入了带有小写名称的常量(它似乎是 JDK 1.0,因为Color
该类的 Java API 规范中没有“Since”表示法),而大写名称是后来在 JDK 1.4 中添加的。
Probably the addition of the uppercase named constants were added to make the constant names in the Color
class consistent with the Code Conventions for the Java Programming Languagewhich in Section 9: Naming Conventionsstate that constants should be in all uppercase letters.
可能添加大写的命名常量是为了使类中的常量名称Color
与Java 编程语言的代码约定一致,其中第 9 节:命名约定规定常量应全部使用大写字母。
Resources
资源
For more information on how to use Frames, the following resources from The Java Tutorialswould be of interest:
有关如何使用 Frames 的更多信息,您可能会对The Java Tutorials 中的以下资源感兴趣:
How to Make Frames- information on general about how to make Frames.
How to Use Root Panes- more specific information about panes, including an illustration of how the different panes relate to each other.
- Using Top-Level Containers
回答by MeBigFatGuy
You want to set the color of the JFrame's content pane.
您想设置 JFrame 内容窗格的颜色。
Other than name there's no difference between the upper and camel case color constants.
除了名称之外,大写和驼峰式颜色常量之间没有区别。
回答by rmartinus
This should work:
这应该有效:
JFrame mainFrame = new JFrame();
mainFrame.setSize(100, 100);
mainFrame.getContentPane().setBackground(Color.CYAN);
mainFrame.setVisible(true);