Java full screen background color wont change?

跟風遠走 提交于 2019-12-02 04:08:30
public void paint(Graphics g){
    g.drawString("This is gonna be awesome", 200, 200);
}

The painting of the background is done in the paint() method. Your overrode the method and didn't invoke super.paint(g) so the background never gets painted.

However, this is NOT the way to do custom painting. You should NOT override the paint() method of a JFrame. If you want to do custom painting then override the paintComponent() method of a JPanel and then add the panel to the frame.

Read the section from the Swing tutorial on Custom Painting for more information.

Edit:

Once you add the super.paint(g), child components of the frame will be painted. This means the content pane gets painted and the content pane is painted over the frame so you won't see the background of the frame, so you also need to add:

//setBackground(Color.PINK);
getContentPane().setBackground(Color.PINK);

The painting of the background is done in the paint function. So, you have to invoke super.paint(g) at the start of the paint function.
Also, you need to override the setBackground function.
So the code becomes:

public void paint(Graphics g){
    super.paint(g);
    g.drawString("This is gonna be awesome", 200, 200);
}

public void setBackground(Color color){
    super.setBackground(color);
    getContentPane().setBackground(color);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!