Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Background in a JPanel

Status
Not open for further replies.

petold

Programmer
Sep 18, 2002
9
SE
Im stuck with this problem that should be an easy one to solve, even for me :) but I cant figure it out. Need someone to take a look at the code and maybe slap my face because I cant make this out....lol.

I whant the background in ths JLabel to be white and not the default grey. Ive tried the method setBackground() but that doesn´t made the trick. Please help this out for me.

The code:
----------------------------------------------------------
//Class that creates an AboutLabel.
public class AboutLabel extends JFrame {

private JLabel label;

public AboutLabel ()
{
super( "J.E.R.C" );

Container c = getContentPane();
c.setLayout (new FlowLayout () );

label = new JLabel();
label.setText( "J.E.R.C version 1.0" );
Icon JERC = new ImageIcon("JERC.gif");
setIconImage(new ImageIcon("ikon.gif").getImage());
label.setIcon(JERC);
label.setHorizontalTextPosition (SwingConstants.CENTER);
label.setVerticalTextPosition (SwingConstants.BOTTOM);
label.setOpaque(true);
label.setIconTextGap(10);
c.add ( label );

Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screen = tk.getScreenSize();
int screenHeight = (int)screen.getHeight();
int screenWidth = (int)screen.getWidth();

setBounds((screenWidth/2) - this.getWidth()/2, (screenHeight/2) - this.getHeight()/2, 150, 150);
//setBackground ( Color.white );????Dont work!!
setResizable(false);
setVisible(true);
}
}
-----------------------------------------------------------

Any help would be very appreciated...

Thx Peter.
 
Hi,

what happens when you put:
this.setBackground ( Color.white );

Rgrds,

T.
 
Do you want the background in the JLabel white, or the JFrame?
 


"this.setBackground ( Color.white );"

Nope, tried that.....dont work.

"Do you want the background in the JLabel white, or the JFrame?"

I guess it is the JFrame that souhld be white.

More suggestions?

//Peter

 
I think what happens is that the layout manager overlays the frame background with a container. If you do this.setBackground(Color.red) you'll see the red flash briefly while the frame is building, and (at least on mine) a thin stripe of red at the bottom afterwards.

If you do setLayoutManager(null) then setting the background works.

Another workaround is to create a JPanel to cover the entire frame and set all your components on it rather then directly on the frame. Then set the background color of the panel.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top