Java Reference
In-Depth Information
Exercise 11.2 Find the documentation for class JFrame . What is the purpose of the
" ImageViewer" parameter that we used in the constructor call above?
Concept:
Components are
placed in a frame
by adding them to
the frame's menu
bar , or content
pane.
A frame consists of three parts: the title bar , an optional menu bar, and the content pane
(Figure 11.3). The exact appearance of the title bar depends on the underlying operating system.
It usually contains the window title and a few window controls.
Figure 11.3
Different parts of a
frame
7LWOHEDU
0HQXEDU
&RQWHQWSDQH
)UDPH
The menu bar and the content pane are under the control of the application. To both, we can add
some components to create a GUI. We shall concentrate on the content pane first.
11.4.2
Adding simple components
Immediately following creation of the JFrame , the frame will be invisible and its content pane
will be empty. We continue by adding a label to the content pane:
Container contentPane = frame.getContentPane();
JLabel label = new JLabel("I am a label.");
contentPane.add(label);
The first line gets the content pane from the frame. We always have to do this; GUI components
are added to a frame by adding them to the frame's content pane. 2
The content pane itself is of type Container . A container is a Swing component that can hold
arbitrary groups of other components—rather like an ArrayList can hold an arbitrary collec-
tion of objects. We shall discuss containers in more detail later.
2
Java also has an add method for adding components directly in the JFrame class, which will also add
the component to the content pane. However, we need access to the content pane for other reasons later
anyway, so we do the adding of components to the content pane at this point as well.
 
Search WWH ::




Custom Search