This discussion looks at several of the facilities provided by the Java AWT for creating user interfaces, placing UI objects in those interfaces, and drawing on them. These issues will be approached from the standpoint of contexts in which objects can be placed and in which text and graphical shapes drawn.The two contexts in which a user interface may be constructed are Panels and Frames.
Panels
don't have borders and are displayed within the window provided by a browser or other application (e.g., the Appletviewer).Panel
is also the superclass ofApplet
, so all applets inherently include aPanel
.
Frames
are a subclass ofWindow
and are displayed within a separate window, with its own border, close spot, etc.
![]()
Java AWT Class Hierarchy. Branch in which Panel
andFrame
appear.The discussions that follow are organized as a tutorial. Issues are discussed incrementally and cumulative through seven steps. The discussion begins with
Panels
and finishes withFrames
. In several places, concepts that will be considered in more detail in later lessons but are needed here are included with minimal explanation.
Step 1
Step 1 creates several user interface components and adds them to the applet's inherent panel. Components include aTextArea
and aButton
.Step 2
Step 2 prints text and draws several graphical shapes on the panel.Step 3
Step 3 draws on the applet's panel and adds UI components to it. Thus, it combines code from Steps 1 & 2.Step 4
A better approach would be to use two panels: one for the UI components and the other for drawing. This is done in Step 4.Step 5
To provide more space for the second panel, Step 5 begins exploring the use ofLayout Managers
and the nesting of panels within panels.Step 6
Step 6 illustrates subclassing, orextending
, thePanel
class in order to control which of two panels is drawn on.Step 7
The six previous steps have worked within the context of the browser's window. There are times, however, when it is desirable for the applet to have its own window. This can be done by using aFrame
as the outer container, rather than aPanel
.
The discussion has focused on basic tools for creating user interface components and drawing on the screen. A number of other UI components are available. Some of these will be discussed in subsequent lessons.A further limitation is that while several interface components (i. e., a
TextArea
and aButton
) were created in the preceding steps, nothing was done with them. In the next lesson, you will see how to determine when a button has been pressed or how to get the text entered into a TextArea.
References useful for this discussion include the following::