Java Reference
In-Depth Information
17.1
JFrames
17.1.1
The basics of JFrames
As you know from Chap. 1, an instance of class
JFrame
is a window that can
appear on your monitor (See Fig. 17.1). The window has two visible parts: the
border
and the
content pane
. The border contains the title bar and resize control
(in the lower right corner). The title bar contains the title and the usual maximize,
minimize, and close buttons. The content pane contains GUI components such as
buttons, scrollbars, and text areas —whatever the program has placed on it.
Activity
17-1.1
Creating a JFrame
The first statement below declares a variable
jf
and assigns to it the name
of a new instance of class
JFrame
, with title
"example"
. The
JFrame
window is
initially not visible. The second statement,
jf.pack()
, tells the
JFrame
to “lay
out” all the components that have been placed in it. The third statement makes
the window visible. (The
JFrame
of Fig. 17.1 has no components.)
JFrame jf=
new
JFrame("example");
jf.pack();
jf.setVisible(
true
);
You can set the size and location of the window. Statement
jf.setSize(200, 70);
changes the window to a width of
200
pixels and a height of
70
pixels.
Statement:
jf.setLocation(60, 30);
moves the window so that its top left corner is at horizontal pixel
60
and vertical
pixel
30
. To retrieve the current width and height of
jf
, use function calls:
jf.getWidth()
and
jf.getHeight()
To retrieve the position of the top-left corner, use function calls:
jf.getX()
and
jf.getY()
Figure 17.1:
A JFrame
Search WWH ::
Custom Search