Java Reference
In-Depth Information
The simple program below shows all of these steps. It produces the empty frame
shown in
Figure 21
.
The
JFrame
class is a part of the
javax.swing
package. Swing is the nickname
for the graphical user interface library in Java. The Ȓxȓ in
javax
denotes the fact that
Swing started out as a Java extension before it was added to the standard library.
We will go into much greater detail about Swing programming in
Chapters 3
,
9
,
10
,
and
18
. For now, consider this program to be the essential plumbing that is required to
show a frame.
ch02/emptyframe/EmptyFrameViewer.java
1
import
javax.swing.JFrame;
2
3
public class
EmptyFrameViewer
4 {
5
public static void
main(String[] args)
6 {
7 JFrame frame =
new
JFrame();
8
9 frame.setSize(
300
,
400
);
10 frame.setTitle(
ÐAn Empty FrameÑ
);
11 frame.setDefaultCloseOperation(JFrame.EXIT_ON
12
13 frame.setVisible(
true
);
14 }
15 }
S
ELF
C
HECK
27.
How do you display a square frame with a title bar that reads ȒHello,
World!ȓ?
28.
How can a program display two frames at once?
2.12 Drawing on a Component
This section continues the optional graphics track. You will learn how to make shapes
appear inside a frame window. The first drawing will be exceedingly modest: just two