问题描述
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!
此外,有人可以指出为什么我似乎有所有颜色一式两份(有一个颜色。 CYAN和Color.cyan)。这两者之间有什么不同吗?也许旧版本是Java之前的枚举,第二个来自Enum?
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?
谢谢
推荐答案
为什么窗户不是预期的青色?
这里的问题是显示 JFrame
内容的区域实际上是内容窗格,而不是 JFrame 本身。
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.
因此,以下行:
mainFrame.setBackground(Color.CYAN);
正在更改
JFrame
的颜色,但实际上并不是在显示 JFrame
时立即可见的部分。
Is changing the color of the
JFrame
, but that is actually not the part which is immediately visible when the JFrame
is displayed.
所需要的是更改所谓的内容窗格*的颜色(请参阅进行说明),将上面的行更改为以下内容:
What is needed is to change the color of what is called the "content pane* (please refer to How to Use Root Panes for an illustration), by changing the above line to the following:
mainFrame.getContentPane().setBackground(Color.CYAN);
在Swing中使用Frames可能会令人惊讶地不直观开始,所以我强烈建议您查看我在此答案底部列出的资源。
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.
<$ c之间是否存在差异$ c> Color.CYAN
和 Color.cyan
?
Is there a difference between Color.CYAN
and 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
类时引入了带小写名称的常量介绍了(似乎是JDK 1.0,因为 Color 类的Java API规范中没有Since表示法),并且稍后添加了大写名称在JDK 1.4中。
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
中生成常量名称与一致的类说明常量应该在所有大写字母。
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 Language which in Section 9: Naming Conventions state that constants should be in all uppercase letters.
资源
有关如何使用框架的更多信息,来自会引起人们的兴趣:
For more information on how to use Frames, the following resources from The Java Tutorials would be of interest:
-
- 有关如何制作框架的一般信息。
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.
这篇关于JFrame.setBackground()不工作 - 为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!