Java 为 JFrame 设置背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1081486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:19:50  来源:igfitidea点击:

Setting background color for a JFrame

javaswinggraphicsawt

提问by

Just how do you set the background color for a JFrame?

您如何为 JFrame 设置背景颜色?

回答by Brandon E Taylor

Retrieve the content pane for the frame and use the setBackground()method inherited from Componentto change the color.

检索框架的内容窗格并使用继承自ComponentsetBackground()方法更改颜色。

Example:

例子:

myJFrame.getContentPane().setBackground( desiredColor );

回答by John T

You can use a container like so:

您可以像这样使用容器:

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

You must of course import java.awt.Colorfor the redcolor constant.

你当然必须进口java.awt.Color颜色不变。

回答by iwanttoprogram

To set the background color for JFrame:

为 JFrame 设置背景颜色:

getContentPane().setBackground(Color.YELLOW);  //Whatever color

回答by Abdullah

Here's another method:

这是另一种方法:

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

I'm using netbeans ide. For me, JFrame.getContentPane()didn't run. I used JFrame.getContentPane()'s class equivalent this.getContentPane.

我正在使用 netbeans ide。对我来说,JFrame.getContentPane()没有跑。我使用JFrame.getContentPane()的类等效this.getContentPane

回答by Pratik K

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);
        }
}

回答by Red_Hat

Probably the SIMPLEST method is this:

可能最简单的方法是这样的:

super.setBackground(Color.CYAN);

You must extend JFrame in the class before doing this!

在执行此操作之前,您必须在类中扩展 JFrame!

回答by Mahdi Hasanpour

you can override the paint method of JFrame and then fill that by your favorite color like this:

你可以覆盖 JFrame 的paint方法,然后用你喜欢的颜色填充它,如下所示:

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

回答by T04435

Hello There I did have the same problem and after many attempts I found that the problem is that you need a Graphics Objectto be able to draw, paint(setBackgroundColor).

你好,我确实遇到了同样的问题,经过多次尝试,我发现问题是你需要一个图形对象才能绘制,paint(setBackgroundColor)。

My code usually goes like this:

我的代码通常是这样的:

import javax.swing.*;
import java.awt.*;


public class DrawGraphics extends JFrame{

    public DrawGraphics(String title) throws HeadlessException {
      super(title);
      InitialElements();
    }

    private void InitialElements(){
      setSize(300, 250);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
      // This one does not work
      // getContentPane().setBackground(new Color(70, 80, 70));

    }

    public void paint(Graphics draw){
      //Here you can perform any drawing like an oval...
      draw.fillOval(40, 40, 60, 50);

      getContentPane().setBackground(new Color(70,80,70));
    }
}

The missing part on almost all other answers is where to place the code. Then now you know it goes in paint(Graphics G)

几乎所有其他答案中缺少的部分是放置代码的位置。那么现在你知道它进入油漆(图形G)

回答by darx

using:

使用:

setBackground(Color.red);

doesn't work properly.

不能正常工作。

use

Container c = JFrame.getContentPane();

c.setBackground(Color.red);

or

或者

myJFrame.getContentPane().setBackground( Color.red );

回答by dhaval joshi

Create a JLabel, resize it so it covers your JFrame. Right Click the JLabel, Find Icon and click on the (...) button. Pick a picture by clicking the Import to project button, then click finish. In the Navigator pane, (Bottom left by default, if disabled go to the Windows tab of your Netbeans IDE and enable it.)

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

using Jlable you can set Background color as well as image also.

使用 Jlable 您还可以设置背景颜色和图像。