Why does the JPanel color not change?

I am trying to make the background color of my program white (instead of gray).

I have a frame class that contains a program (a new instance of this frame class is created in the main method). I have a constructor:

this.setBackground(Color.WHITE); 

Just in case, it was somehow blocked by other panels, I added the same line to the designer of each panel that I have in my program (and I mean each one).

However, nothing happened.

What could be wrong?

+4
source share
1 answer

If this code is called in the JFrame constructor, you do not change the color of the JPrame contentPane that must be executed. Make this call on the contentpane page:

 getContentPane().setBackground(Color.WHITE); 

Other JPanels added to the GUI should not have their own color set, but should have their opaque property set to false through myPanel.setOpaque(false); . This will allow you to see any image or color behind them. The exception is contentPane, which should always be opaque.

If this does not help, you need to tell us more (you need to do this anyway, since your question does not contain a lot of necessary and important information).

+6
source

Source: https://habr.com/ru/post/1393091/


All Articles