Unit 3 Introducing The AWT
Unit 3 Introducing The AWT
com
Unit-3/ Java Programming-II
AWT Classes
The AWT classes are contained in the java.awt package. It is one of Java’s largest packages.
The AWT package contains different classes :
GUI Component classes, such as Button, TextField, and Label.
GUI Container classes, such as Frame and Panel.
Layout managers, such as FlowLayout, BorderLayout and GridLayout.
Event Handling Classes, such as AWTEvent, AWTEventMultiCaster
Custom graphics classes, such as Graphics, Color and Font
#AWT Classes are rarely used now days because of its platform dependent and heavy weight
nature.
Hierarchy of AWT classes:
https://genuinenotes.com
https://genuinenotes.com
Unit-3/ Java Programming-II
Window Fundamentals
The AWT defines windows according to a class hierarchy that adds functionality and
specificity with each level.
The two most common windows are those derived from Panel, which is used by applets,
and those derived from Frame, which creates a standard application window.
Component
At the top of the AWT hierarchy is the Component class. Component is an abstract class that
encapsulates all of the attributes of a visual component. Except for menus, all user interface
elements that are displayed on the screen and that interact with the user are subclasses of
Component. A Component object is responsible for remembering the current foreground and
background colors and the currently selected text font.
Components are elementary GUI entities, such as Button, Label, and TextField.
Container
The Container class is a subclass of Component. It has additional methods that allow other
Component objects to be nested within it. Other Container objects can be stored inside of a
Container (since they are themselves instances of Component). This makes for a multileveled
containment system. A container is responsible for laying out (that is, positioning) any
components that it contains. It does this through the use of various layout managers.
Containers, such as Frame and Panel, are used to hold components in a specific layout (such
as FlowLayout or GridLayout). A container can also hold sub-containers.
Each GUI program has a top-level container. The commonly-used top-level containers in
AWT are Frame, Dialog and Applet.
Secondary containers are placed inside a top-level container or another secondary container.
(Panel, ScrollPane)
Panel
The Panel class is a concrete subclass of Container. Panel is the superclass for Applet. When
screen output is directed to an applet, it is drawn on the surface of a Panel object. In essence, a
Panel is a window that does not contain a title bar, menu bar, or border.
https://genuinenotes.com
https://genuinenotes.com
Unit-3/ Java Programming-II
Window
The Window class creates a top-level window. A top-level window is not contained within any
other object; it sits directly on the desktop. Generally, you won’t create Window objects directly.
Instead, you will use a subclass of Window called Frame
Frame
Frame encapsulates what is commonly thought of as a “window.” It is a subclass of Window and
has a title bar, menu bar, borders, and resizing corners.
A Frame provides the "main window" for your GUI application. It has a title bar (containing
an icon, a title, the minimize, maximize/restore-down and close buttons), an optional menu
bar, and the content display area.
Canvas
Although it is not part of the hierarchy for applet or frame windows, there is one other type of
window that we will find valuable: Canvas. Derived from Component, Canvas encapsulates a
blank window upon which we can draw.
https://genuinenotes.com
https://genuinenotes.com
Unit-3/ Java Programming-II
The new size of the window is specified by newWidth and newHeight, or by the
width and height fields of the Dimension object passed in newSize. The
dimensions are specified in terms of pixels.
The getSize( ) method is used to obtain the current size of a window. One of its forms is
shown here:
Dimension getSize( )
This method returns the current size of the window contained within the width and height fields
of a Dimension object.
Hiding and Showing a Window
After a frame window has been created, it will not be visible until you call setVisible( ). Its
signature is shown here:
void setVisible(boolean visibleFlag)
The component is visible if the argument to this method is true. Otherwise, it is hidden.
Setting a Window’s Title
You can change the title in a frame window using setTitle( ), which has this general form:
void setTitle(String newTitle)
Here, newTitle is the new title for the window.
Closing a Frame Window
When using a frame window, your program must remove that window from the screen when it is
closed, by calling setVisible(false). For this event handling should be implemented.
https://genuinenotes.com
https://genuinenotes.com
Unit-3/ Java Programming-II
package awtexample;
import java.awt.*;
https://genuinenotes.com
https://genuinenotes.com
Unit-3/ Java Programming-II
While it is possible to simply create a window by creating an instance of Frame, it is not so useful
to do so, because you will not be able to do much with it. For example, you will not be able to
receive or process events that occur within it or easily output information to it. Most of the time,
you will create a subclass of Frame. Doing so lets you override Frame’s methods and provide event
handling.
https://genuinenotes.com
https://genuinenotes.com
Unit-3/ Java Programming-II
https://genuinenotes.com
https://genuinenotes.com
Unit-3/ Java Programming-II
Output :
https://genuinenotes.com