0% found this document useful (0 votes)
15 views

3.AWT and Event Handling

Uploaded by

Shweta Sahani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

3.AWT and Event Handling

Uploaded by

Shweta Sahani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

AWT And EvEnT

HAndling

Java provides a powerful framework for building graphical


user interfaces (GUIs) through the Abstract Window Toolkit
(AWT). This toolkit facilitates the creation of rich, interactive
applications by allowing developers to design interfaces that
respond to user actions. In this overview, we will discuss the
key concepts of AWT, including layout management, event
handling mechanisms, event models, and graphics, as well as
a brief introduction to HTML basics and applet classes.
1. Introduction to AWT
The Abstract Window Toolkit (AWT) is Java’s original
platform-dependent GUI toolkit. AWT provides a set of APIs
that enable developers to create graphical user interfaces for
Java applications. It offers a wide range of components,
including buttons, text fields, labels, and more, which can be
arranged and customized to create interactive interfaces.
Key Features of AWT:
• Platform Independence: While AWT components rely on
the underlying operating system for rendering, they
maintain Java's principle of "write once, run anywhere."
• Lightweight Components: AWT components are
referred to as heavyweight components because they
rely on native system resources, unlike Swing
components, which are lightweight and rendered by
Java.

2. Layout Manager
A Layout Manager is an interface that defines how
components are arranged in a container. Java provides
several layout managers, each offering different ways to
position and size components. Choosing the appropriate
layout manager is crucial for creating a visually appealing and
user-friendly interface.
Common Layout Managers:
• FlowLayout: Arranges components in a left-to-right flow,
much like words in a paragraph. If the container is too
narrow, the components wrap to the next line.
• BorderLayout: Divides the container into five areas:
north, south, east, west, and center. Components can be
added to these specific regions, allowing for flexible
arrangements.
• GridLayout: Arranges components in a grid format,
where each component occupies an equal amount of
space in a specified number of rows and columns.
• CardLayout: Allows multiple components to occupy the
same space, switching between them like a stack of
cards. This is useful for tabbed interfaces or wizard-like
forms.
3. Event Handling Mechanism
The event handling mechanism in AWT allows applications to
respond to user actions, such as mouse clicks, keyboard
inputs, or window events. Understanding this mechanism is
essential for creating interactive applications.
Event Model
The event model in AWT is based on the observer design
pattern, where objects (event sources) notify other objects
(event listeners) when an event occurs. The model consists of
the following components:
• Event Sources: Objects that generate events. For
example, a button can be an event source when it is
clicked.
• Event Listeners: Interfaces that define methods to
handle specific events. An event listener must be
registered with an event source to receive notifications
about events.
Event Classes
Java provides a hierarchy of event classes that represent
various types of events. Each event class encapsulates
information about the event, such as the source of the event
and additional details relevant to that event type.
Common event classes include:
• ActionEvent: Represents an action performed by the
user, such as clicking a button.
• MouseEvent: Captures mouse-related actions, like
mouse clicks, movements, or releases.
• KeyEvent: Represents keyboard actions, such as key
presses or releases.
4. Sources of Events
The sources of events are components that generate events
in response to user interactions. Examples of event sources
include:
• Buttons: Generate ActionEvent when clicked.
• Text Fields: Generate ActionEvent when the user presses
Enter.
• Mouse: Generates MouseEvent when a mouse button is
pressed or released.
• Windows: Generate events when they are opened,
closed, minimized, or resized.
5. Event Listener Interfaces
Event listener interfaces define the methods that need to be
implemented to handle specific events. When an event
occurs, the corresponding method in the listener interface is
invoked.
Common Event Listener Interfaces:
• ActionListener: Handles action events (e.g., button
clicks).
• MouseListener: Handles mouse events (e.g., mouse
clicks, entering/exiting a component).
• KeyListener: Handles keyboard events (e.g., key presses,
releases).
• WindowListener: Handles window events (e.g., closing,
opening, activating).
To respond to an event, a class must implement the
appropriate listener interface and register it with the event
source.

6. AWT: Working with Windows


AWT allows developers to create windows and dialogs that
can serve as containers for components. The main window of
an AWT application is created using the Frame class.
Key Points:
• Frames: A top-level window with a title and a border.
Frames can contain other components and can be
resized, minimized, or closed.
• Dialogs: A temporary window that prompts the user for
input or displays information. Dialogs can be modal
(blocking input to other windows) or non-modal.
Working with Windows
Developers can create and manipulate windows by setting
properties such as size, visibility, and layout. Proper
management of window events is essential for creating a
smooth user experience.
7. AWT Controls
AWT provides various controls (components) that allow users
to interact with the application. Common AWT controls
include:
• Button: Triggers an action when clicked.
• Label: Displays a non-editable text message.
• TextField: Allows users to input single-line text.
• TextArea: Allows users to input multi-line text.
• Checkbox: Represents a binary choice (checked or
unchecked).
• List: Displays a list of items from which users can select
one or more options.
These controls can be combined and customized to create
interactive user interfaces.
8. HTML Basic Tags
In addition to Java, understanding basic HTML tags is
essential for web development. HTML (HyperText Markup
Language) is the standard language for creating web pages.
Common HTML Tags:
• <html>: The root element of an HTML page.
• <head>: Contains metadata, including the title of the
document.
• <body>: Contains the content of the web page, including
text, images, and links.
• <h1> to <h6>: Header tags for defining headings of
different levels.
• <p>: Defines a paragraph of text.
• <a>: Creates a hyperlink to another page or resource.
• <img>: Embeds an image in the document.
Familiarity with HTML tags is beneficial for integrating Java
applets and web content.
9. Applet Classes
Java applets are small applications that run in a web browser.
They are created using the Applet class, which is part of the
AWT package. Applets can be used to create dynamic web
content, providing interactive experiences for users.
Key Points:
• Applets are initialized through the init() method, where
setup actions take place.
• The start() method is called when the applet becomes
visible, allowing it to begin executing.
• Applets can be embedded in HTML pages using the
<applet> tag (though deprecated in favor of the <object>
tag).

10. Graphics
AWT also provides capabilities for drawing graphics on the
screen. The Graphics class offers methods for rendering
shapes, text, and images.
Key Features:
• Drawing Shapes: Methods like drawLine(), drawRect(),
and drawOval() allow developers to create various
shapes.
• Filling Shapes: Methods like fillRect() and fillOval() fill
shapes with color.
• Rendering Text: The drawString() method allows text to
be displayed on the screen.
Developers can override the paint() method of a component
to implement custom drawing logic.

You might also like