0% found this document useful (0 votes)
12 views67 pages

First Lec AWT

The document provides an overview of Graphical User Interfaces (GUIs) in Java, focusing on the Abstract Window Toolkit (AWT) as the original GUI toolkit. It discusses key components, advantages, and limitations of AWT, as well as its architecture, including frames, panels, and event handling. Additionally, it covers the hierarchy and common methods of various AWT components like buttons, text fields, and checkboxes.

Uploaded by

wel24065
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views67 pages

First Lec AWT

The document provides an overview of Graphical User Interfaces (GUIs) in Java, focusing on the Abstract Window Toolkit (AWT) as the original GUI toolkit. It discusses key components, advantages, and limitations of AWT, as well as its architecture, including frames, panels, and event handling. Additionally, it covers the hierarchy and common methods of various AWT components like buttons, text fields, and checkboxes.

Uploaded by

wel24065
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 67

(AWT)

JAVA GUI
HOME

BY
Maryam O. Albosyifi
INTRODUCTION

GUI
INTRODUCTION

A Graphical User Interface (GUI) is a


type of user interface that allows users
to interact with software using graphical
elements like windows, icons, buttons, and
menus instead of text-based commands.
INTRODUCTION Create a folder using command
Display folder contents using
command

The folder has been created


INTRODUCTION

GUI
INTRODUCTION
Key Components of a GUI
INTRODUCTION

Windows: Frames or containers for UI elements.

Icons: Represent applications, files, or commands visually.

Menus: Allow users to execute commands via dropdowns or lists.

Buttons: Trigger actions when clicked.

Text Fields: Accept user input in text form.


INTRODUCTION
Advantages of GUI
INTRODUCTION

User-Friendly: Simplifies interaction for non-technical users.

Visual Appeal: Attractive and engaging interfaces.

Error Reduction: Visual cues reduce user errors.

Efficiency: Quick access to functions and commands.

Standardization: Familiarity across applications due to standard GUI conventions.


INTRODUCTION
Key Frameworks in Java

AWT (Abstract Window Toolkit)


INTRODUCTION

The first GUI toolkit for Java.

Lightweight but lacks modern components.

Swing
Built on AWT.

Offers rich UI components.

Platform-independent and more flexible.


AWT
Abstract Window Toolkit
AWT (Abstract Window Toolkit)
Definition

The Abstract Window Toolkit (AWT) is Java’s original windowing, graphics, and user interface toolkit.

It provides a set of APIs for creating and managing GUI components


like windows, buttons, and text fields. The same
AWT

example was run


Key Characteristics on a different OS
theme.

Platform Dependent: AWT components rely on native system resources.

Simple UI Elements: Provides basic UI components like buttons, labels, and text areas.

Part of JDK: Introduced with Java 1.0.

All its classes are part of the Java.awt package.


AWT (Abstract Window Toolkit)

Advantages

Lightweight: Ideal for simple GUI applications.

Platform Independence: Applications run across platforms.


AWT

Disadvantages

Limited customization options compared to Swing or JavaFX.

Heavily dependent on native system libraries, leading to inconsistent


behavior across platforms..
AWT
AWT Hierarchy
AWT Architecture

Key Components

Containers
Hold and organize components.

Types:
AWT

Frame: Top-level window of GUI applications.


Panel: Sub-container used for grouping components.

Basic Components
Interactive elements like:
Types:

Label: Displays text.


TextArea: For multi-line text input.
Button: Triggers actions when clicked.
Checkbox: Enables selecting/deselecting options.
TextField: Allows single-line text input.
AWT Architecture

Key Components

Layout Managers
Arrange components within a container.
Types:

FlowLayout: Places components in a row, left to right.


AWT

BorderLayout: Divides container into five regions (North, South, East, West, Center).
GridLayout : Arranges components in a grid of rows and columns.

BoxLayout: Stacks components vertically or horizontally.

Advantages of Layout Managers

Automatically adjust component sizes.


Simplify GUI design for different screen sizes.
AWT Architecture

Key Components

Event Handling
AWT’s event-driven model captures and responds to user interactions like
AWT

clicks, key presses, or mouse movements.


.
AWT Architecture

Limitations of AWT

Platform Dependency: Relies on native OS components.


Limited Features: No modern UI components.
Inconsistent Behavior: May behave differently on various platforms.
AWT

Alternatives to AWT

Swing: Enhanced version of AWT.


JavaFX: For modern, feature-rich applications.
AWT Frame
What is an AWT Frame in Java?
In Java AWT (Abstract Window Toolkit), a frame is part of the AWT package and is used to build graphical
user interfaces (GUIs) in Java.

It is a subclass of java.awt.Container .
A top-level container that represents a window on the screen.
AWT

It can hold components like buttons, labels, text fields, etc.

Hierarchy of AWT Frame:


The Frame class is part of the java.awt package and has the following inheritance:

java.lang.Object
↳ java.awt.Component
↳ java.awt.Container
↳ java.awt.Window
↳ java.awt.Frame
AWT Frame

Constructors of Frame

Frame)( : Creates a new, invisible frame with no title.


Frame(String title) : Creates a new, invisible frame with the specified title.
AWT
AWT Frame
The output:
AWT
AWT Frame

Key Methods of Frame Class :


Visibility and Lifecycle

Method Description
setVisible(boolean b) Makes the frame visible (true) or invisible (false).
isVisible() Returns true if the frame is visible.
AWT

dispose() Releases system resources and destroys the frame.


setTitle(String title) Sets the title of the frame.
getTitle() Returns the title of the frame.
AWT Frame
Key Methods of Frame Class :
Size and Position

Method Description
setSize(int width, int height) Sets the size (width and height) of the frame.
getSize() Returns the current size of the frame as a Dimension object.
AWT

setLocation(int x, int y) Sets the position of the frame on the screen.


getLocation() Returns the current position of the frame as a Point object.
pack() Resizes the frame to fit its components.
AWT Frame
Layout and Components

Method Description
setLayout(LayoutManager mgr) Sets the layout manager for arranging components.
add(Component comp) Adds a component to the frame.
add(Component comp, Object constraints) Adds a component with layout constraints.
AWT

remove(Component comp) Removes the specified component from the frame.


removeAll() Removes all components from the frame.

Window Events

Method Description
addWindowListener(WindowListener l) Adds a listener to handle window events (close, minimize).
setResizable(boolean resizable) Allows or disallows resizing of the frame.
isResizable() Returns true if the frame is resizable.
AWT Frame

Other Methods

Method Description
setBackground(Color c) Sets the background color of the frame.
setForeground(Color c) Sets the foreground color of the frame.
setCursor(Cursor cursor) Sets the cursor displayed when hovering over the frame.
AWT
AWT
AWT Frame
AWT Architecture

The output:
AWT
AWT Frame
In Java AWT, you can create a GUI using a Frame in two main ways:
By Extending the Frame Class (Inheritance)
This involves creating a subclass of Frame and customizing it.

The output:
AWT
AWT Frame
In Java AWT, you can create a GUI using a Frame in two main ways:

By Creating an Object of the Frame Class (Association)

This involves using a Frame instance directly and customizing it.

The output:
AWT
AWT Frame

Comparing both approaches

Feature Inheritance Association


Must recreate functionality in each
Code Reusability Easy to reuse frame behavior via subclass.
AWT

instance.
Can override Frame methods for advanced
Customization Limited to using Frame methods as-is.
behavior.
More straightforward for smaller
Simplicity May be overkill for simple GUIs.
programs.
Best for complex GUIs with shared
Use Case Best for quick, simple GUIs.
behavior.
ADD Method

The add() method is not part of the Component class itself but is found in its subclass
Container (part of java.awt) .

add() Method Purpose:


AWT

Only Containers Can Use add()

The add() method is used to add a Component (like a button, text field, etc.) to a Container (like a panel,
frame, or another container .

Since all GUI components ultimately derive from Component, you can add any child component into a
container using this method.
Add Method
add() Method overloaded:

The add() method has several overloaded versions in the Container class:

add(Component comp(:

Adds the specified component to the container.


AWT

add(Component comp, int index(

Adds the specified component at a specified index (position) .


add(Component comp, Object constraints(

Adds the specified component to the container with constraints (e.g., layout
manager constraints like BorderLayout, GridBagConstraints) .
AWT Panel

What is an AWT Panel in Java?


In Java AWT, the Panel class is a container that can hold and organize components:

It is a lightweight component and typically used within other containers like Frame .
Panel class provides several methods inherited from Container and Component classes, along with its
AWT

own specific functionality

Hierarchy of AWT Panel


The Panel class is part of the java.awt package and is a subclass of Container

java.lang.Object
↳ java.awt.Component
↳ java.awt.Container
↳ java.awt.Panel
↳ java.applet.Applet (subclass of Panel)
AWT Panel

Constructors of Panel:

Panel)(
Creates a new panel with a default layout manager (FlowLayout).

Panel(LayoutManager layout)
AWT

Creates a new panel with the specified layout manager.


AWT Panel

Commonly Used Methods of Panel Class :

Method Description
add(Component comp) Adds a component to the panel.

setLayout(LayoutManager mgr) Sets the layout manager for arranging components in the panel.
AWT

getLayout() Returns the layout manager used by the panel.


setBackground(Color c) Sets the background color of the panel.
setForeground(Color c) Sets the foreground color of the panel.
setVisible(boolean b) Sets the visibility of the panel.

addMouseListener(MouseListener l) Adds a mouse listener to the panel for handling mouse events.

addKeyListener(KeyListener l) Adds a key listener to the panel for handling keyboard events.

remove(Component comp) Removes a specific component from the panel.


removeAll() Removes all components from the panel.
AWT Panel

Commonly Used Methods of Panel Class :

Method Description
setLayout(LayoutManager layout) To set a specific LayoutManager.

setLocation(int x, int y) To set Panel Location


AWT
AWT Panel

Simple Panel
AWT

The output:
AWT
AWT Panel

Multi Panel The output:


Toolkit Class
java.awt package provides a way to interact with the native system environment trough Toolkit class and its
methods. Through this toolkit, you can access various system properties and resources like screen dimensions,
fonts, and more.
Example
We can control where the container is displayed relative to the screen dimensions by getting the screen
dimensions and then specifying the container display points (X,Y) .
AWT

Toolkit dTK = Toolkit.getDefaultToolkit();

This is a part of the java.awt package. The variable holds This method returns the

It provides a way to interact with the a reference to the default toolkit for the

native system environment. default toolkit. application.


Toolkit Class

Dimension dim = dTK.getScreenSize();

To instantiate an object that Holds the This method of Toolkit


stores the width and height of screen's width retrieves the dimensions of
the screen in pixels. and height. the primary screen.
AWT

dim.width : width of the screen


dim.height: height of the screen

Using these two values, the position of the frame can be determined relative to the width and height of the
screen.
AWT
Toolkit Class
Toolkit Class
The output:

The frame in the center of screen


AWT
AWT Components
The Component class in Java AWT (Abstract Window Toolkit) is the root class for all visual
elements in a graphical user interface (GUI) that can be displayed on the screen. It serves as
a superclass for GUI components like buttons, labels, panels, etc.

Hierarchy of Component Class:


AWT

Object
↳ java.awt.Component
↳ java.awt.Container
↳ javax.swing.JComponent

Note: All AWT and Swing components (e.g., JButton, JPanel, etc.) ultimately extend the
Component class, either directly or indirectly.
Labels
A Label is a non-editable text component used to display a string or message .
Hierarchy: java.lang.Object
↳ java.awt.Component
↳ java.awt.Label
Constructors:
AWT

Label(): Creates an empty label.


Label(String text): Creates a label with the specified text.
Label(String text, int alignment): Creates a label with the specified text and alignment
(Label.LEFT, Label.CENTER, Label.RIGHT).

Common Methods:
setText(String text): Sets the label text.
getText(): Returns the label text.setAlignment(int
alignment): Sets the alignment of the label.
getAlignment(): Returns the alignment of the label.
Labels

The output:
AWT
Buttons
A Button is a component that triggers an action when clicked .

Hierarchy: java.lang.Object
↳ java.awt.Component
↳ java.awt.Button
Constructors:
AWT

Button(): Creates an empty button.


Button(String label): Creates a button with the specified label.

Common Methods:
setLabel(String label): Sets the button label.
getLabel(): Returns the button label.
addActionListener(ActionListener l): Adds an action listener to the button.
AWT
Buttons

The output:
Text Fields
A TextField is a single-line text input component.

Hierarchy: java.lang.Object
↳ java.awt.Component
↳ java.awt.TextComponent

Constructors: ↳ java.awt.TextField
AWT

TextField(): Creates an empty text field.


TextField(String text): Creates a text field with the specified initial text.
TextField(int columns): Creates a text field with the specified number of columns.
TextField(String text, int columns): Creates a text field with specified text and columns.
Common Methods:
setText(String text): Sets the text of the text field.
getText(): Gets the text of the text field.
setEditable(boolean b): Makes the text field editable or read-only.
Text Fields

The output:
AWT
Text Areas
A TextArea is a multi-line text input component.

Hierarchy: java.lang.Object
↳ java.awt.Component
↳ java.awt.TextComponent

Constructors: ↳ java.awt.TextArea
AWT

TextArea(): Creates an empty text area.


TextArea(String text): Creates a text area with the specified initial text.
TextArea(int rows, int columns): Creates a text area with the specified size.
TextArea(String text, int rows, int columns): Creates a text area with specified text and size
Common Methods:
setText(String text): Sets the text of the text area.
getText(): Gets the text of the text area.
append(String text): Appends text to the end of the current text.
Text Areas

The output:
AWT
Check Boxes
A Checkbox is a component that represents a box that can be checked or unchecked.

Hierarchy: java.lang.Object
↳ java.awt.Component
↳ java.awt.Checkbox
Constructors:
AWT

Checkbox(): Creates an empty checkbox.


Checkbox(String label): Creates a checkbox with the specified label.
Checkbox(String label, boolean state): Creates a checkbox with specified label and initial

Common Methods:
setState(boolean state): Sets the state of the checkbox.
getState(): Gets the state of the checkbox.
Check Boxes

The output:
AWT
Radio Buttons

Java AWT does not have a dedicated RadioButton class. Instead, radio buttons are
implemented using Checkbox combined with a CheckboxGroup.
When checkboxes are grouped, only one can be selected at a time, functioning as a radio
button.

Constructors:
AWT

Checkbox(String label, boolean state, CheckboxGroup group) :Creates a checkbox with the specified
label, state, and group for radio button functionality.

Common Methods:
setCheckboxGroup(CheckboxGroup group): Sets the group to which the checkbox belongs.
getCheckboxGroup(): Returns the group of the checkbox.
AWT
Radio Buttons

The output:
Combo Boxes (Choice in AWT)
A Choice component is used to create a dropdown list where users can select one option
from a predefined list.
Hierarchy: java.lang.Object
↳ java.awt.Component
↳ java.awt.Choice
Constructor:
AWT

Choice():Creates an empty dropdown list.

Common Methods:
add(String item): Adds an item to the dropdown list.
getItem(int index): Returns the item at the specified index.
getSelectedIndex(): Returns the index of the selected item.
getSelectedItem(): Returns the selected item.
remove(String item): Removes the specified item from the dropdown.
remove(int index): Removes the item at the specified index.
Combo Boxes (Choice in AWT)
AWT

The output:
Lists
A List component displays a list of items from which the user can select one or multiple.

Hierarchy:
java.lang.Object
↳ java.awt.Component
AWT

↳ java.awt.List

Constructors:
List(): Creates a single-selection list.
List(int rows): Creates a list with the specified number of visible rows.
List(int rows, boolean multipleMode): Creates a list with the specified number of rows and multiple-
selection mode..
Lists

Common Methods:
add(String item): Adds an item to the list.
getSelectedItem(): Returns the selected item (for single-selection lists).
getSelectedItems(): Returns an array of selected items (for multi-selection lists).
select(int index): Selects the item at the specified index.
AWT

deselect(int index): Deselects the item at the specified index.


setMultipleMode(boolean b): Enables or disables multiple-selection mode.
Lists

The output:
AWT
Methods of the Component Class

Key Methods of the Component Class

Size Management:

Method Description
AWT

setBounds(int x, int y, int width, int height) Sets the location and size of the component.
setSize(int width, int height) Sets the size of the component (in pixels).
getSize() Returns the size of the component as a Dimension object.
setLocation(int x, int y) Sets the location of the component relative to its parent.
getLocation() Returns the current location of the component as a Point object.
Methods of the Component Class

Key Methods of the Component Class

Visibility and Enablement:

Method Description
AWT

setVisible(boolean b) Sets the visibility of the component.


isVisible() Returns true if the component is visible.
setEnabled(boolean b) Enables or disables the component.
isEnabled() Returns true if the component is enabled.
Methods of the Component Class

Key Methods of the Component Class

Accessibility and Name Management:


AWT

Method Description
setName(String name) Sets a name for the component.
getName() Returns the name of the component.
AWT Methods of the Component Class

Note: The example will be available on the Class room.


AWT Methods of the Component Class
AWT Methods of the Component Class
AWT Methods of the Component Class
AWT Methods of the Component Class
AWT Methods of the Component Class
Methods of the Component Class
The output:
AWT
Any question ?

You might also like