First Lec AWT
First Lec AWT
JAVA GUI
HOME
BY
Maryam O. Albosyifi
INTRODUCTION
GUI
INTRODUCTION
GUI
INTRODUCTION
Key Components of a GUI
INTRODUCTION
Swing
Built on AWT.
The Abstract Window Toolkit (AWT) is Java’s original windowing, graphics, and user interface toolkit.
Simple UI Elements: Provides basic UI components like buttons, labels, and text areas.
Advantages
Disadvantages
Key Components
Containers
Hold and organize components.
Types:
AWT
Basic Components
Interactive elements like:
Types:
Key Components
Layout Managers
Arrange components within a container.
Types:
BorderLayout: Divides container into five regions (North, South, East, West, Center).
GridLayout : Arranges components in a grid of rows and columns.
Key Components
Event Handling
AWT’s event-driven model captures and responds to user interactions like
AWT
Limitations of AWT
Alternatives to AWT
It is a subclass of java.awt.Container .
A top-level container that represents a window on the screen.
AWT
java.lang.Object
↳ java.awt.Component
↳ java.awt.Container
↳ java.awt.Window
↳ java.awt.Frame
AWT Frame
Constructors of Frame
Method Description
setVisible(boolean b) Makes the frame visible (true) or invisible (false).
isVisible() Returns true if the frame is visible.
AWT
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
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
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:
The output:
AWT
AWT Frame
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) .
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 with constraints (e.g., layout
manager constraints like BorderLayout, GridBagConstraints) .
AWT Panel
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
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
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
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.
Method Description
setLayout(LayoutManager layout) To set a specific LayoutManager.
Simple Panel
AWT
The output:
AWT
AWT Panel
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
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:
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
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
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
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
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
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
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
The output:
AWT
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
Method Description
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