为什么 JPanel 背景不改变颜色?

发布于 2024-12-28 19:12:50 字数 241 浏览 3 评论 0原文

我试图将程序的背景颜色设置为白色(而不是灰色)。

我有一个包含程序的框架类(该框架类的新实例在 main 方法中实例化)。我有以下是构造函数:

this.setBackground(Color.WHITE);

为了以防万一它被其他面板遮挡,我在程序中的每个面板(我的意思是每个面板)的构造函数中添加了相同的行。

然而,什么也没发生。

可能出什么问题了?

I'm trying to make the background color of my program white (instead of gray).

I have a frame class that contains the program (a new instance of that frame class is instantiated in the main method). I have the following is the constructor:

this.setBackground(Color.WHITE);

Just in case it was somehow being blocked out by other panels, I added the same line to the constructor of every panel I have in my program (and I mean every one).

Still, nothing happened.

What could be wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怕倦 2025-01-04 19:12:50

如果在 JFrame 的构造函数中调用该代码,则您不会更改 JFrame 的 contentPane 的颜色,而这是需要做的。在 contentPane 上进行该调用:

getContentPane().setBackground(Color.WHITE);

添加到 GUI 的其他 JPanel 不应设置其颜色,但应通过 myPanel.setOpaque(false); 将其 opaque 属性设置为 false。它们背后的图像或颜色可见。 contentPane 是个例外,它应该始终是不透明的。

如果这没有帮助,您需要告诉我们更多信息(无论如何您都需要这样做,因为您的问题遗漏了许多必要且重要的细节)。

If that code is called in a JFrame's constructor you are not changing the JFrame's contentPane's color which is what needs to be done. Make that call on the contentPane:

getContentPane().setBackground(Color.WHITE);

The other JPanels added to the GUI shouldn't have their colors set, but should have their opaque property set to false via myPanel.setOpaque(false); This will let any image or color behind them to be visible. The exception to this is the contentPane which should always be opaque.

If this doesn't help, you need to tell us more (you need to do this anyway as your question leaves out much necessary and important detail).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文