简体   繁体   English

NetBeans 不会改变我的背景颜色

[英]NetBeans Won't Change my Background Color

So, I'm like 15 minutes new to NetBeans, and I'm already having an issue.所以,我刚接触 NetBeans 15 分钟,我已经遇到了问题。 So, I just want to make a simple GUI, and I've got all that good, except for the background.所以,我只想制作一个简单的 GUI,除了背景之外,我已经做好了。 I want the background to be yellow, and as we can see here, it is:我希望背景是黄色的,正如我们在这里看到的,它是:

窗口属性

Those are the window properties.这些是窗口属性。 And in the code it confirms the RGB selection of mine with this line: setBackground(new java.awt.Color(255, 255, 55));在代码中,它用这一行确认了我的 RGB 选择: setBackground(new java.awt.Color(255, 255, 55));

However, when the window is launched, I get this:但是,当窗口启动时,我得到了这个:

申请窗口

Why is my choice of color not showing up in the background?为什么我选择的颜色没有显示在背景中?

based on this link :基于此链接

Create a new project and insert a new JFrame.创建一个新项目并插入一个新的 JFrame。 Go to the properties window and set the background property of JFrame form to red.转到属性窗口并将 JFrame 表单的背景属性设置为红色。 If you run the application now, the window just flashes red and then becomes gray again.如果您现在运行该应用程序,窗口只会闪烁红色,然后再次变为灰色。

The reason is that a code generated in "initComponents" function is:原因是在“initComponents”函数中生成的一段代码是:

1) setBackground(new java.awt.Color(57, 214, 18)); 1) setBackground(new java.awt.Color(57, 214, 18));

But 99% of programmers would probably expect the behavior of code:但是 99% 的程序员可能会期望代码的行为:

2) getContentPane().setBackground(new java.awt.Color(57, 214, 18)); 2) getContentPane().setBackground(new java.awt.Color(57, 214, 18));

Simply, the JFrame is covered with some other container (content pane), so if you want to set up the background color for the JFrame, you need to do it with writing code manually.简单地说,JFrame 被其他一些容器(内容窗格)覆盖,因此如果您想为 JFrame 设置背景颜色,您需要手动编写代码来完成。

Well, you might create a JPanel, add it to the JFrame and set its background color.好吧,您可以创建一个 JPanel,将其添加到 JFrame 并设置其背景颜色。

Panel panel = new Panel(frame.getWidth(), frame.getHeight());
panel.setBackground(new Color(255, 255, 255));
frame.add(panel);

在 jframe 顶部创建一个面板并更改面板的背景颜色对我有用。

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

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