Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在JPanel';行不通_Java_Jpanel - Fatal编程技术网

Java 在JPanel';行不通

Java 在JPanel';行不通,java,jpanel,Java,Jpanel,我正在开发一个“类似绘画”的应用程序(一个小型绘图软件),以熟悉Java2D组件。这是我的问题:我有一个JFrame,它的ContentPane是从JPanel继承的类的实例。我想将背景颜色设置为白色,但它仍保持默认颜色。。。与ContentPane对应的类名是Container。下面是一个简化的代码: public class Container extends JPanel { public Container() { super(); this.s

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

public class Container extends JPanel {

    public Container() {
        super();
        this.setBackground(Color.WHITE);
    }
}
JFrame构造函数包含以下行:

this.setContentPane(mainContainer);
我错过什么了吗

Thx.

这可以解决它

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

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

c.setOpaque(true);

我也遇到了这个问题,唯一的解决办法就是OP所建议的

// Only this works for me
this.setBackground(Color.blue);
示例类的全部代码都在这里(只是为了显示我试图放置/设置setBackground()的位置)


当我添加行:jFrame.setBackground(Color.WHITE);它可以工作…不过,我想了解为什么它不能与我的contentPane一起工作。有什么想法吗?Thx。我也有同样的问题!我特别希望能够单击我的按钮并交替颜色,但这根本不起作用添加对
super.paintComponent(g)的调用;
在重写方法中正确绘制JPanel:DIt不会像您认为的那样工作。您会将该语句放在哪里?如果您将代码更改为:
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);//…}
应按预期工作;蓝色面板和白色圆圈(如果我正确理解了意图)
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);
    }
}