设置JFrame的背景颜色

问题描述 投票:50回答:14

您如何设置JFrame的背景色?

java swing graphics awt
14个回答
70
投票

检索框架的内容窗格,并使用从setBackground()继承的Component方法来更改颜色。

示例:


0
投票
public nameOfTheClass() { final Container c = this.getContentPane(); public void actionPerformed(ActionEvent e) { c.setBackground(Color.white); } }

0
投票
从停滞状态恢复线程。

在2018年,此解决方案适用于NetBeans中的Swing / JFrame(应该在任何IDE中都可以使用:):


-1
投票
import java.awt.*; import javax.swing.*; public class MySimpleLayout extends JFrame { private Container c; public MySimpleLayout(String str) { super(str); c=getContentPane(); c.setLayout(null); c.setBackground(Color.WHITE); } }

-1
投票

创建一个JLabel,调整其大小,使其覆盖您的JFrame。右键单击JLabel,查找图标,然后单击(...)按钮。通过单击导入到项目按钮来选择图片,然后单击完成。在“导航器”窗格中,(默认情况下,左下角如果被禁用,请转到Netbeans IDE的Windows选项卡并启用它。)


-5
投票
可能是SIMPLEST方法是这样的:

super.setBackground(Color.CYAN);


35
投票

设置JFrame的背景色:


11
投票

使用:


4
投票

这是最简单和正确的方法。您要做的就是在initComponents();


4
投票

要设置JFrame的背景色,请尝试以下操作:


3
投票

你好,我确实遇到了同样的问题,经过多次尝试,我发现问题是你需要一个[[Graphics Object


2
投票
您可以像这样使用容器:

Container c = JFrame.getContentPane(); c.setBackground(Color.red);


2
投票
这里是另一种方法:

private void RenkMouseClicked(java.awt.event.MouseEvent evt) { renk = JColorChooser.showDialog(null, "Select the background color", renk); Container a = this.getContentPane(); a.setBackground(renk); }


0
投票
您可以重写JFrame的paint方法,然后用您喜欢的颜色填充它,如下所示:

@Override public void paint(Graphics g) { g.setColor(Color.red); g.fillRect(0, 0, this.getWidth(), this.getHeight()); }

© www.soinside.com 2019 - 2024. All rights reserved.