Java Reference
In-Depth Information
Display 17.11
Using Panels
(part 4 of 4)
R
ESULTING
GUI (After clicking Gray button)
Adding Color
Color a
JFrame
as follows:
JFrame_Object
.getContentPane().setBackground(
Color
);
If this is inside a constructor for the
JFrame
, then the expression simplifies to
getContentPane().setBackground(
Color
);
or the equivalent
this
.getContentPane().setBackground(
Color
);
Color a button, label, or any other component (which is not a
JFrame
) as follows:
Component_Object
.setBackground(
Color
);
Note that
getContentPane()
is only used with a
JFrame
.
EXAMPLE (INSIDE A CONSTRUCTOR FOR A DERIVED CLASS OF
JFrame
)
getContentPane().setBackground(Color.WHITE);
JButton redButton =
new
JButton("Red");
redButton.setBackground(Color.RED);
The
Container
Class
The class called
Container
is in the
java.awt
package. Any descendent class of
the class
Container
can have components added to it (or, more precisely, can have
components added to objects of the class). The class
JFrame
is a descendent class of the
Container