您如何设置JFrame的背景色?
检索框架的内容窗格,并使用从setBackground()继承的Component方法来更改颜色。
示例:
public nameOfTheClass() {
final Container c = this.getContentPane();
public void actionPerformed(ActionEvent e) {
c.setBackground(Color.white);
}
}
在2018年,此解决方案适用于NetBeans中的Swing / JFrame(应该在任何IDE中都可以使用:):
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);
}
}
创建一个JLabel,调整其大小,使其覆盖您的JFrame。右键单击JLabel,查找图标,然后单击(...)按钮。通过单击导入到项目按钮来选择图片,然后单击完成。在“导航器”窗格中,(默认情况下,左下角如果被禁用,请转到Netbeans IDE的Windows选项卡并启用它。)
super.setBackground(Color.CYAN);
设置JFrame的背景色:
使用:
这是最简单和正确的方法。您要做的就是在initComponents();
要设置JFrame的背景色,请尝试以下操作:
你好,我确实遇到了同样的问题,经过多次尝试,我发现问题是你需要一个[[Graphics Object
Container c = JFrame.getContentPane();
c.setBackground(Color.red);
private void RenkMouseClicked(java.awt.event.MouseEvent evt) {
renk = JColorChooser.showDialog(null, "Select the background color",
renk);
Container a = this.getContentPane();
a.setBackground(renk);
}
@Override
public void paint(Graphics g) {
g.setColor(Color.red);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
}