java - Unable to set JPanel's Background in my Swing program. -


i set jpanel contentpane of jframe.

when use:

jpanel.setbackground(color.white); 

the white color not applied.

but when use:

jframe.setbackground(color.white); 

it works... surprised behaviour. should opposite, shouldn't it?

sscce:

here sscce:

main class:

public class main {     public static void main(string[] args) {         window win = new window();     } } 

window class:

import java.awt.color; import javax.swing.jframe;  public class window extends jframe {     private container maincontainer = new container();      public window(){         super();         this.settitle("my paint");         this.setsize(720, 576);         this.setlocationrelativeto(null);         this.setresizable(true);         this.setdefaultcloseoperation(jframe.exit_on_close);         maincontainer.setbackground(color.white); //doesn't work whereas this.setbackground(color.white) works         this.setcontentpane(maincontainer);         this.setvisible(true);     } }   

container class:

import java.awt.graphics; import javax.swing.jpanel;  public class container extends jpanel {     public container() {         super();     }     public void paintcomponent(graphics g) {      } } 

the reason simple include following line

super.paintcomponent(g); 

when override paintcomponent.

public void paintcomponent(graphics g) {          super.paintcomponent(g);     } 

now works perfectly.

you should unless have specific reason .

[ps:change colour red or darker notice difference becomes difficult differentiate between jframe's default grey colour , white colour]


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -