在JPanel上设置Java中的背景颜色不起作用 [英] Set background color in Java on a JPanel doesn't work

查看:1962
本文介绍了在JPanel上设置Java中的背景颜色不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个类似绘画的应用程序(一个小绘图软件)来熟悉Java 2D组件。这是我的问题:我有一个JFrame,其ContentPane是继承自JPanel的类的实例。我想将背景颜色设置为白色,但它仍保持默认颜色...... ContentPane对应的类名称为Container。这是一个简化的代码:

I am working on a "paint-like" application (a little drawing software) to familiarize with Java 2D components. Here is my problem: I have a JFrame whose ContentPane is an instance of a class inheriting from JPanel. I want to set the background color to white but it remains on its default color... The class name corresponding to ContentPane is Container. Here is a simplified code:

public class Container extends JPanel {

    public Container() {
        super();
        this.setBackground(Color.WHITE);
    }
}

JFrame构造函数包含以下行:

The JFrame constructor contains the line:

this.setContentPane(mainContainer);

我错过了什么吗?

Thx 。

推荐答案

这可以修复它......

This could fix it...

public class Container extends JPanel
{
    public Container() 
    {
        super();
        this.setOpaque(true);
        this.setBackground(Color.WHITE);
    }
}

这篇关于在JPanel上设置Java中的背景颜色不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆