JFrame未设置背景颜色。

huangapple go评论97阅读模式
英文:

Jframe Not Setting Background Color

问题

我尝试了很多不同的方法来使我的项目显示背景颜色它仍然不会改变颜色我知道这只是需要修复的小问题但我找不到它在哪里非常感谢您的帮助。(我在顶部有导入不确定 Stack 是否显示

import java.awt.Color; //导入代码所需的内容
import java.awt.Graphics; //导入代码所需的内容
import javax.swing.JFrame; //导入代码所需的内容

public class bigJavaClass extends JFrame{  //JFrame 是查看器,因此我们可以看到我们正在绘制什么
    private static final long serialVersionUID = 1L;
    public static final int WIDTH = 800;
    public static final int HEIGHT = 600;
    public static final int R = 200;
    public static final int G = 200;
    public static final int B = 200;

    public static void main(String[] args) {
        bigJavaClass test = new bigJavaClass();
        test.setSize(WIDTH, HEIGHT);
        test.setResizable(false);
        test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        test.setVisible(true);
        test.setBackground(new Color(248, 177, 149)); //背景颜色 R、G、B
    }

    public void paint(Graphics g){
        boolean a = false; //变量 a
        boolean b = false; //变量 b
        boolean c = false; //变量 c
        boolean d = false; //变量 d
英文:

I have tried a lot of different things to get my project to display background color. It still won't change the color. I know it's something little that just needs to be fixed but I can't find it. Any help is appreciated. (I do have the imports at the top not sure if Stack shows that)
-Liam

import java.awt.Color; //importing things that the code needs 
import java.awt.Graphics; //importing things that the code needs 
import javax.swing.JFrame; //importing things that the code needs 





public class bigJavaClass extends JFrame{  //Jframe is the viewer so we can see what we are drawing 
    private static final long serialVersionUID = 1L;
    public static final int WIDTH = 800;
    public static final int HEIGHT = 600;
    public static final int R = 200;
    public static final int G = 200;
    public static final int B = 200;

public static void main(String[] args) {
    bigJavaClass test = new bigJavaClass();
    test.setSize(WIDTH, HEIGHT);
    test.setResizable(false);
    test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    test.setVisible(true);
    test.setBackground(new Color (248, 177, 149)); //background color R, G, B
}

    public void paint(Graphics g){
	    boolean a = false; //for the variable a 
	    boolean b = false; //for the variable b
	    boolean c = false; //for the variable c 
	    boolean d = false; //for the variable d 

答案1

得分: 0

  1. 类名不应以小写字符开头。

  2. 不要在JFrame上覆盖paint()方法。这会导致丢失默认的绘制行为,即绘制背景和添加到框架中的组件。

  3. “内容窗格”会绘制在框架上方。因此,您需要更改内容窗格的背景。有关框架结构的更多信息,请参阅:使用顶级容器。从教程中下载工作示例并自行调整,以了解如何更好地构建您的代码结构。

英文:
  1. class names should NOT start with a lower case character.

  2. Don't override paint() on a JFrame. You have lost the default painting behaviour, which is to paint the background and components added to the frame.

  3. The "content pane" is painted on top of the frame. So you will need to change the background of the content pane. See: Using Top Level Containers for more information about the structure of the frame. Download working examples from the tutorial and customize to learn how to better structure your code.

答案2

得分: -1

在使窗口可见之前设置背景。

英文:

Set background before making the frame visible.

huangapple
  • 本文由 发表于 2020年10月26日 21:51:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64538483.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定