3.AWT and Event Handling
3.AWT and Event Handling
HAndling
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.
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.