Java Reference
In-Depth Information
2.
Which container does not require the use of a content pane when adding compo-
nents to it?
a.
JPanel
b.
JWindow
c.
JFrame
3.
If you use
setSize()
on an application's main frame or window, where will it
appear on your desktop?
a.
At the center of the desktop
b.
At the same spot the last application appeared
c.
At the upper-left corner of the desktop
9
Answers
1. b.
A
JTextArea
requires a container to support scrolling, but it is not a container
itself.
2. a.
JPanel
is one of the simple containers that is not subdivided into panes, so you
can call its
add(
Component
)
method to add components directly to the panel.
3. c.
This is a trick question—calling
setSize()
has nothing to do with a window's
position on the desktop. You must call
setBounds()
rather than
setSize()
to
choose where a frame will appear.
Certification Practice
The following question is the kind of thing you could expect to be asked on a Java pro-
gramming certification test. Answer it without looking at today's material or using the
Java compiler to test the code.
Given:
import javax.swing.*;
public class Display extends JFrame {
public Display() {
super(“Display”);
// answer goes here
JLabel hello = new JLabel(“Hello”);
JPanel pane = new JPanel();
pane.add(hello);
setContentPane(pane);
pack();
setVisible(true);