if my code is like this, background is not changed....
class MyFrame extends JFrame
{
MyFrame()
{
setBackground(Color.red);
}
}
but if i use .....
class MyFrame extends JFrame
{
MyFrame()
{
getContentPane().setBackground(Color.red);
}
}
background is now changed..... i am using java 1.6..
is it important to use getContentPane() or is there any other way to change the background of JFrame?
saurabh agr wrote:
background is now changed..... i am using java 1.6..
is it important to use getContentPane() or is there any other way to change the background of JFrame?
I think the safest bet is to work with the contentPane, but to be careful that components added to it have the appropriate opaque setting so as not to draw over the image.
I can confirm having just tried it with JAVA 1.6 that you need to use getContentPane().setBackground(Color.red);
Normaly with this version of JAVA the compiler will imply in some cases that you mean to refer to the property of the ContentPane (a JPanel instance) rather than the JFrame itself, to provide a sort of shortcut to the programer.
However as you've just seen this isn't a good habbit to get into because it doesn't work with every method of a JPanel as you have illistrated and your code will not work if compiled on older versions of JAVA in anycase.
Just to add it's also not a good idea because it causes confusion over what object's method you are really calling as well.
When evil is afoot and you don't have any arms you gotta be hip and do the legwork, but always kick some ... tiny ad:
a bit of art, as a gift, that will fit in a stocking