Java Reference
In-Depth Information
Nowadays, most Java GUI programming is done by using Swing. We will
still discuss the AWT in this chapter, though, because it is an important part of
GUI programming, and many of the AWT classes are used in Swing, including
the layout managers, and event-handling classes and interfaces. After you
understand the way a GUI is created, you will find that using AWT and Swing
is the same in terms of developing the code. For example, creating and using a
Button in AWT is very similar to creating and using a JButton, Swing's version
of a GUI button. And the event-handling code behind the scenes is exactly the
same, no matter if you are using a Button or JButton.
The names of the Swing classes all begin with a capital J, like JButton.
For the most part, an AWT program can be converted to a Swing program
by adding a capital J to the class names used in the source code and
recompiling the code.
Creating Windows
The basic starting point of a GUI is the container because you need a container
before you can start laying out your components. The java.awt.Frame and
javax.swing.JFrame classes are containers that represent a basic window with
a title bar and common windowing capabilities such as resizing, minimizing,
maximizing, and closing. The Frame class is used for AWT programs and is the
parent class of JFrame, which is used for Swing programs.
java.awt.Frame Class
When working with Frame objects, there are basically three steps involved to
get a Frame window to appear on the screen:
1.
Instantiate the Frame object in memory.
2.
Give the Frame object a size using setSize(), setBounds(), or pack().
3.
Make the Frame appear on the screen by invoking setVisible(true).
Let's look at instantiating a Frame object first. The java.awt.Frame class has
four constructors:
public Frame(). Creates a new frame with no message in the title bar.
public Frame(String title).
Creates a new frame with the given String
appearing in the title bar.
Search WWH ::




Custom Search