简体   繁体   English

在JPanel上设置Java中的背景颜色不起作用

[英]Set background color in Java on a JPanel doesn't work

I am working on a "paint-like" application (a little drawing software) to familiarize with Java 2D components. 我正在研究一个“类似绘画”的应用程序(一个小绘图软件)来熟悉Java 2D组件。 Here is my problem: I have a JFrame whose ContentPane is an instance of a class inheriting from JPanel. 这是我的问题:我有一个JFrame,其ContentPane是继承自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. 我想将背景颜色设置为白色,但它保持默认颜色... ContentPane对应的类名是Container。 Here is a simplified code: 这是一个简化的代码:

public class Container extends JPanel {

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

The JFrame constructor contains the line: JFrame构造函数包含以下行:

this.setContentPane(mainContainer);

Have I missed something? 我错过了什么吗?

Thx. 谢谢。

This could fix it... 这可以解决它......

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

Some components' background is switched off by default. 默认情况下,某些组件的背景会关闭。 The background color is only applied to opaque widgets. 背景颜色仅适用于不透明的小部件。 Call the following method for all components in your widget hierarchie that should paint its background: 对于应该绘制其背景的窗口小部件中的所有组件,请调用以下方法:

c.setOpaque(true);

I also had that problem, and only that worked out is exactly what OP suggested. 我也遇到了这个问题,只有这个问题与OP建议完全一致。

// Only this works for me
this.setBackground(Color.blue);

Whole code of example class is here (just for purpose to show where I tried to put/set setBackground(); 示例类的整个代码在这里(仅用于显示我尝试放置/设置setBackground()的位置;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class CircleDraw extends JFrame {
    Float diameter = 150f;

    public CircleDraw() {
        super("Circle Draw");
        this.setSize(300, 300);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.add(new CirclePanel(diameter));
        this.setVisible(true);

        // Only this works for me
        this.setBackground(Color.blue);
    }

    public static void main(String[] args) {
        new CircleDraw();
    }
}

class CirclePanel extends JPanel {

    Float diameter;

    public CirclePanel(Float diameter) {
        super();
        // this.setOpaque(true);
        // this.setBackground(Color.WHITE);
        this.diameter = diameter;
    }

    @Override
    public void paintComponent(Graphics g) {

        int panelWidth = this.getSize().width;
        int panelHeight = this.getSize().height;

        setPreferredSize(new Dimension(300, 300));
        Graphics2D comp2D = (Graphics2D) g;
        comp2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        comp2D.setStroke(new BasicStroke(1f));
        // comp2D.setBackground(Color.white);
        comp2D.setPaint(Color.white);

        Ellipse2D.Float e1 = new Ellipse2D.Float((panelWidth / 2) - (diameter / 2), (panelHeight / 2) - (diameter / 2), diameter, diameter);
        comp2D.draw(e1);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM