JPanel setBackground(Color.BLACK) does nothing

前端 未结 6 1155
不知归路
不知归路 2021-01-04 12:29

I have the folowing custom JPanel and I have aded it to my frame using Netbeans GUI builder but the background won\'t change! I can see the circle, drawing with g.fillOval()

6条回答
  •  无人及你
    2021-01-04 12:47

    You have to call the super.paintComponent(); as well, to allow the Java API draw the original background. The super refers to the original JPanel code.

    public void paintComponent(Graphics g){
        super.paintComponent(g);
    
        g.setColor(Color.red);
        g.fillOval(player.getxCenter(), player.getyCenter(), player.getRadius(), player.getRadius());
    }
    

提交回复
热议问题