【问题标题】:Set background color in Java on a JPanel doesn't work在 JPanel 上的 Java 中设置背景颜色不起作用
【发布时间】:2013-07-31 09:36:40
【问题描述】:

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

public class Container extends JPanel {

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

JFrame 构造函数包含以下行:

this.setContentPane(mainContainer);

我错过了什么吗?

谢谢。

【问题讨论】:

  • 当我添加行时:jFrame.setBackground(Color.WHITE);它有效......不过,我想了解为什么它不适用于我的 contentPane。任何想法?谢谢。

标签: java jpanel


【解决方案1】:

这可以解决它...

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

【讨论】:

  • 我也有同样的问题!我特别希望能够单击我的按钮并交替颜色,但这根本不起作用
【解决方案2】:

默认情况下,某些组件的背景是关闭的。背景颜色仅适用于不透明的小部件。为小部件层次结构中应绘制其背景的所有组件调用以下方法:

c.setOpaque(true);

【讨论】:

    【解决方案3】:

    我也遇到过这个问题,只有解决了 OP 建议的问题。

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

    示例类的完整代码在这里(只是为了显示我尝试放置/设置 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);
        }
    }
    

    【讨论】:

    • 在覆盖方法中添加对super.paintComponent(g);的调用以正确绘制JPanel:D
    • 它不会像你想象的那样工作。你会把那句话准确地放在哪里?
    • 如果您将代码更改为:public CircleDraw() { // ... } public static void main(String[] args) { new CircleDraw(); } public CirclePanel(Float diameter) { super(); this.setOpaque(true); this.setBackground(Color.BLUE); this.diameter = diameter; } @Override public void paintComponent(Graphics g) { super.paintComponent(g); // ... } 这应该可以按预期工作;蓝色面板和白色圆圈(如果我理解正确的意图)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-04
    • 1970-01-01
    • 2022-01-05
    • 2017-12-20
    • 1970-01-01
    • 2014-07-20
    相关资源
    最近更新 更多