SlideShare a Scribd company logo
GRAPHICAL USER
INTERFACE
WITH AWT
Event Handling
 An event can be defined as changing the state of
an object or behavior by performing actions.
Actions can be a button click, cursor movement,
keypress through keyboard or page scrolling,
etc.
 The java.awt.event package can be used to
provide various event classes.
Event Handling
• Foreground Events - Those events which require the direct
interaction of user. They are generated as consequences of
a person interacting with the graphical components in
Graphical User Interface. For example, clicking on a button,
moving the mouse, entering a character through keyboard,
selecting an item from list, scrolling the page etc.
• Background Events - Those events that require the
interaction of end user are known as background events.
Operating system interrupts, hardware or software failure,
timer expires, an operation completion are the example of
background events.
Event Handling
 Event Handling is the mechanism that controls the event and decides what should
happen if an event occurs. This mechanism have the code which is known as event
handler that is executed when an event occurs. Java Uses the Delegation Event Model
to handle the events. This model defines the standard mechanism to generate and
handle the events. Let's have a brief introduction to this model.
 The Delegation Event Model has the following key participants namely:
• Source - The source is an object on which event occurs. Source is responsible for
providing information of the occurred event to it's handler. Java provide as with classes
for source object.
• Listener - It is also known as event handler. Listener is responsible for generating
response to an event. From java implementation point of view the listener is also an
object. Listener waits until it receives an event. Once the event is received , the listener
process the event an then returns.
Event Handling
 Steps involved in event handling
• The User clicks the button and the event is generated.
• Now the object of concerned event class is created automatically and
information about the source and the event get populated with in same
object.
• Event object is forwarded to the method of registered listener class.
• the method is now get executed and returns.
 Points to remember about listener
• In order to design a listener class we have to develop some listener
interfaces. These Listener interfaces forecast some public abstract callback
methods which must be implemented by the listener class.
• If you do not implement the any if the predefined interfaces then your class
can not act as a listener class for a source object.
Layout Managers
 The LayoutManagers are used to arrange components in a particular
manner. The Java LayoutManagers facilitates us to control the positioning
and size of the components in GUI forms. LayoutManager is an interface
that is implemented by all the classes of layout managers.
 There are the following classes that represent the layout managers:
1. java.awt.BorderLayout
2. java.awt.FlowLayout
3. java.awt.GridLayout
4. java.awt.CardLayout
5. java.awt.GridBagLayout
6. javax.swing.BoxLayout
7. javax.swing.GroupLayout
8. javax.swing.ScrollPaneLayout
9. javax.swing.SpringLayout etc.
Border Layout
 The BorderLayout is used to arrange the components in five regions:
north, south, east, west, and center. Each region (area) may contain one
component only. It is the default layout of a frame or window. The
BorderLayout provides five constants for each region:
1. public static final int NORTH
2. public static final int SOUTH
3. public static final int EAST
4. public static final int WEST
5. public static final int CENTER
Flow Layout
 The Java FlowLayout class is used to arrange the components in a line, one
after another (in a flow). It is the default layout of the applet or panel.
 Fields of FlowLayout class
1. public static final int LEFT
2. public static final int RIGHT
3. public static final int CENTER
4. public static final int LEADING
5. public static final int TRAILING
Grid Layout
 The Java GridLayout class is used to arrange the components in a
rectangular grid. One component is displayed in each rectangle.
 Constructors of GridLayout class
1. GridLayout(): creates a grid layout with one column per component in a
row.
2. GridLayout(int rows, int columns): creates a grid layout with the given
rows and columns but no gaps between the components.
3. GridLayout(int rows, int columns, int hgap, int vgap): creates a grid
layout with the given rows and columns along with given horizontal and
vertical gaps.
CREATING A SIMPLE WINDOW
start by creating a new Java project in the IDE of your choice (such as NetBeans
or Eclipse).
Create a new Java class and name it something like "MyWindow".
In the class, import the necessary AWT classes using the import statement:
we usually use import java.awt.* ( This library is typically called awt wild
card library because it includes all the basic imports we need )
 Create a new Frame object and set its properties, such as size, location, and
background color:
In AWT, a Frame is used to create a top-level window. A Frame is a subclass of
Window and provides a title bar, borders, and the standard window management
functionality of a typical application window, such as minimizing, maximizing, closing, and
resizing. Therefore, a Frame provides a convenient way to create a window in AWT.
Common Methods
 setBounds(int x, int y, int width, int height): This method is used to set the size and
position of a component within its container. It takes four parameters:
x: The x-coordinate of the top-left corner of the component's bounding box.
y: The y-coordinate of the top-left corner of the component's bounding box.
width: The width of the component.
height: The height of the component.
For example, nameLabel.setBounds(50, 50, 60, 20); sets the bounds of the "Name" label
component with its top-left corner at coordinates (50, 50) in the container, with a width of
60 pixels and a height of 20 pixels.
 setSize(int width, int height): This method is used to set the size of a window or
component. It takes two parameters:
width: The width of the window or component.
height: The height of the window or component.
For example, frame.setSize(400, 300); sets the size of the frame to be 400 pixels wide and
300 pixels tall.
Common Methods
 setTitle(String title): This method is used to set the title of a
window or frame. It takes one parameter:
title: The title to be set for the window or frame.
For example, frame.setTitle("Frame with Components"); sets the
title of the frame to "Frame with Components".
 setVisible(boolean visible): This method is used to set the
visibility of a window or frame. It takes one parameter:
visible: A boolean value indicating whether the window or frame
should be visible (true) or invisible (false).
For example, frame.setVisible(true); makes the frame visible on the
screen.
public class MyWindow extends Frame {
public MyWindow() {
// Set the title of the window
setTitle("My AWT Window");
// Set the size of the window
setSize(500, 300);
// Set the location of the window on the screen
setLocation(100, 100);
// Set the background color of the window
setBackground(Color.WHITE);
// Make the window visible
setVisible(true);
}
}
 In the main method of the class, create an instance of the MyWindow class:
public static void main(String[] args) {
MyWindow myWindow = new MyWindow();
}
ADDING A LABEL
First, create a new instance of the Label class and set its text using the setText
method.
Next, set the position and size of the label using the setBounds method.
Finally, add the label to the frame using the add method.
Label label = new Label("Hello World!");
label.setBounds(50, 50, 100, 20);
add(label);
ADDING A BUTTON
Create an instance of Button class:
Button btn = new Button("Click me");
Here, we have created an instance of the Button class with the label "Click me".
Set the size and position of the button using the setBounds() method:
btn.setBounds(50, 80, 100, 20);
Here, we have set the size of the button to 100x20 and positioned it at (50, 80)
on the window.
Add the button to the window using the add() method:
add(btn);
Here, we have added the button to the window.
ADDING A PANEL
 The Frame class is a top-level container, but it is not suitable for adding
components directly. Instead, we need to create a Panel and add the
components to it, and then add the panel to the frame.
 So, in order to add a button to the window created, we first need to create a
panel, add the button to it, and then add the panel to the frame. Here's an
example code to add a button to the window:
Panel panel = new Panel();
panel.setLayout(null);
panel.add(button);
This code creates a new panel, sets its layout to null (so we can specify the exact
position and size of the button), creates a new button, sets its position and size,
adds the button to the panel, adds the panel to the frame, and finally sets the
frame to be visible.
Add functionality to any specific button
 To add functionality to a specific button in AWT, you need to perform
the following steps:
1. Create an object of the Button class and add it to the Frame or Panel.
2. Implement the ActionListener interface and override the
actionPerformed() method to specify the action to be performed
when the button is clicked.
3. Add an ActionListener to the Button object using the
addActionListener() method.
// Create the button myButton = new Button("Click Me!");
// Add an ActionListener to the button
myButton.addActionListener(this);
// Add the button to the Frame add(myButton);
public void actionPerformed(ActionEvent e)
{ // Perform the action when the button is clicked
System.out.println("Button clicked!"); }
ACTION LISTNER
 In Java, an ActionListener is an interface that is used to handle events triggered by
user actions such as button clicks, menu selections, and list selections. It provides
a way for a program to register itself as an event listener for certain types of
events and then respond appropriately when those events occur.
 An ActionListener typically consists of a single method called actionPerformed().
This method is called automatically by the Java Virtual Machine (JVM) when the
event occurs. Inside the actionPerformed() method, you can define the actions
that should be taken in response to the event.
 To use ActionListener in a program, you need to create an instance of a class that
implements the ActionListener interface and then register that instance as an
event listener for the component that generates the events. For example, if you
want to handle button clicks, you need to create an instance of the ActionListener
class and then register it with the button using the addButtonListener() method.
ACTION LISTNER
In AWT (Abstract Window Toolkit) in Java, the ActionListener interface is commonly used
for handling action events. The ActionListener is part of the java.awt.event package. This
interface is used to respond to events triggered by GUI components, such as buttons.
Here's a brief explanation of ActionListener:
ActionListener:
Interface: java.awt.event.ActionListener
Methods:
void actionPerformed(ActionEvent e): This method is invoked when an action occurs. The
code to be executed when an action (e.g., button click) happens should be written within
this method.
Ad

Recommended

Java_gui_with_AWT_and_its_components.ppt
Java_gui_with_AWT_and_its_components.ppt
JyothiAmpally
 
Basic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in Java
suraj pandey
 
javaprogramming framework-ppt frame.pptx
javaprogramming framework-ppt frame.pptx
DrDGayathriDevi
 
CORE JAVA-2
CORE JAVA-2
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Swing and AWT in java
Swing and AWT in java
Adil Mehmoood
 
Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892
renuka gavli
 
Session 9_AWT in java with all demonstrations.pdf
Session 9_AWT in java with all demonstrations.pdf
tabbu23
 
Windows Programming with AWT
Windows Programming with AWT
backdoor
 
UNIT-2-AJAVA.pdf
UNIT-2-AJAVA.pdf
PriyanshiPrajapati27
 
Abstract Window Toolkit
Abstract Window Toolkit
RutvaThakkar1
 
JAVA AWT
JAVA AWT
shanmuga rajan
 
object oriented programming examples
object oriented programming examples
Abdii Rashid
 
java- Abstract Window toolkit
java- Abstract Window toolkit
Jayant Dalvi
 
TY.BSc.IT Java QB U1
TY.BSc.IT Java QB U1
Lokesh Singrol
 
UNIT-I.pptx awt advance java abstract windowing toolkit and swing
UNIT-I.pptx awt advance java abstract windowing toolkit and swing
utkarshabhope
 
JAVA AWT presentation for awt key events.pptx
JAVA AWT presentation for awt key events.pptx
ShubhamNain11
 
JAVA Programming: Topic -AWT(Abstract Window Tool )
JAVA Programming: Topic -AWT(Abstract Window Tool )
Navya Francis
 
Gui
Gui
Sardar Alam
 
Java awt
Java awt
Arati Gadgil
 
Abstract Window Toolkit_Event Handling_python
Abstract Window Toolkit_Event Handling_python
jasminebeulahg
 
MODULE 5.pptx gui programming and applets
MODULE 5.pptx gui programming and applets
LIKITHLIKITH7
 
28 awt
28 awt
Prachi Vijh
 
14a-gui.ppt
14a-gui.ppt
DrDGayathriDevi
 
GUI components in Java
GUI components in Java
kirupasuchi1996
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024
nehakumari0xf
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024
kashyapneha2809
 
Swing
Swing
Jaydeep Viradiya
 
AWT.pptx
AWT.pptx
MumtazAli889808
 
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
sumadsadjelly121997
 
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 

More Related Content

Similar to Creating GUI.pptx Gui graphical user interface (20)

UNIT-2-AJAVA.pdf
UNIT-2-AJAVA.pdf
PriyanshiPrajapati27
 
Abstract Window Toolkit
Abstract Window Toolkit
RutvaThakkar1
 
JAVA AWT
JAVA AWT
shanmuga rajan
 
object oriented programming examples
object oriented programming examples
Abdii Rashid
 
java- Abstract Window toolkit
java- Abstract Window toolkit
Jayant Dalvi
 
TY.BSc.IT Java QB U1
TY.BSc.IT Java QB U1
Lokesh Singrol
 
UNIT-I.pptx awt advance java abstract windowing toolkit and swing
UNIT-I.pptx awt advance java abstract windowing toolkit and swing
utkarshabhope
 
JAVA AWT presentation for awt key events.pptx
JAVA AWT presentation for awt key events.pptx
ShubhamNain11
 
JAVA Programming: Topic -AWT(Abstract Window Tool )
JAVA Programming: Topic -AWT(Abstract Window Tool )
Navya Francis
 
Gui
Gui
Sardar Alam
 
Java awt
Java awt
Arati Gadgil
 
Abstract Window Toolkit_Event Handling_python
Abstract Window Toolkit_Event Handling_python
jasminebeulahg
 
MODULE 5.pptx gui programming and applets
MODULE 5.pptx gui programming and applets
LIKITHLIKITH7
 
28 awt
28 awt
Prachi Vijh
 
14a-gui.ppt
14a-gui.ppt
DrDGayathriDevi
 
GUI components in Java
GUI components in Java
kirupasuchi1996
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024
nehakumari0xf
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024
kashyapneha2809
 
Swing
Swing
Jaydeep Viradiya
 
AWT.pptx
AWT.pptx
MumtazAli889808
 
Abstract Window Toolkit
Abstract Window Toolkit
RutvaThakkar1
 
object oriented programming examples
object oriented programming examples
Abdii Rashid
 
java- Abstract Window toolkit
java- Abstract Window toolkit
Jayant Dalvi
 
UNIT-I.pptx awt advance java abstract windowing toolkit and swing
UNIT-I.pptx awt advance java abstract windowing toolkit and swing
utkarshabhope
 
JAVA AWT presentation for awt key events.pptx
JAVA AWT presentation for awt key events.pptx
ShubhamNain11
 
JAVA Programming: Topic -AWT(Abstract Window Tool )
JAVA Programming: Topic -AWT(Abstract Window Tool )
Navya Francis
 
Abstract Window Toolkit_Event Handling_python
Abstract Window Toolkit_Event Handling_python
jasminebeulahg
 
MODULE 5.pptx gui programming and applets
MODULE 5.pptx gui programming and applets
LIKITHLIKITH7
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024
nehakumari0xf
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024
kashyapneha2809
 

Recently uploaded (20)

Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
sumadsadjelly121997
 
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 
INDUCTIVE EFFECT slide for first prof pharamacy students
INDUCTIVE EFFECT slide for first prof pharamacy students
SHABNAM FAIZ
 
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
Ultimatewinner0342
 
How to Customize Quotation Layouts in Odoo 18
How to Customize Quotation Layouts in Odoo 18
Celine George
 
LDMMIA Shop & Student News Summer Solstice 25
LDMMIA Shop & Student News Summer Solstice 25
LDM & Mia eStudios
 
LDMMIA Yoga S10 Free Workshop Grad Level
LDMMIA Yoga S10 Free Workshop Grad Level
LDM & Mia eStudios
 
Paper 106 | Ambition and Corruption: A Comparative Analysis of ‘The Great Gat...
Paper 106 | Ambition and Corruption: A Comparative Analysis of ‘The Great Gat...
Rajdeep Bavaliya
 
Code Profiling in Odoo 18 - Odoo 18 Slides
Code Profiling in Odoo 18 - Odoo 18 Slides
Celine George
 
Vitamin and Nutritional Deficiencies.pptx
Vitamin and Nutritional Deficiencies.pptx
Vishal Chanalia
 
NSUMD_M1 Library Orientation_June 11, 2025.pptx
NSUMD_M1 Library Orientation_June 11, 2025.pptx
Julie Sarpy
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
Gladiolous Cultivation practices by AKL.pdf
Gladiolous Cultivation practices by AKL.pdf
kushallamichhame
 
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT Kharagpur Quiz Club
 
Q1_ENGLISH_PPT_WEEK 1 power point grade 3 Quarter 1 week 1
Q1_ENGLISH_PPT_WEEK 1 power point grade 3 Quarter 1 week 1
jutaydeonne
 
This is why students from these 44 institutions have not received National Se...
This is why students from these 44 institutions have not received National Se...
Kweku Zurek
 
Q1_TLE 8_Week 1- Day 1 tools and equipment
Q1_TLE 8_Week 1- Day 1 tools and equipment
clairenotado3
 
A Visual Introduction to the Prophet Jeremiah
A Visual Introduction to the Prophet Jeremiah
Steve Thomason
 
How to Manage Different Customer Addresses in Odoo 18 Accounting
How to Manage Different Customer Addresses in Odoo 18 Accounting
Celine George
 
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
sumadsadjelly121997
 
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 
INDUCTIVE EFFECT slide for first prof pharamacy students
INDUCTIVE EFFECT slide for first prof pharamacy students
SHABNAM FAIZ
 
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
Ultimatewinner0342
 
How to Customize Quotation Layouts in Odoo 18
How to Customize Quotation Layouts in Odoo 18
Celine George
 
LDMMIA Shop & Student News Summer Solstice 25
LDMMIA Shop & Student News Summer Solstice 25
LDM & Mia eStudios
 
LDMMIA Yoga S10 Free Workshop Grad Level
LDMMIA Yoga S10 Free Workshop Grad Level
LDM & Mia eStudios
 
Paper 106 | Ambition and Corruption: A Comparative Analysis of ‘The Great Gat...
Paper 106 | Ambition and Corruption: A Comparative Analysis of ‘The Great Gat...
Rajdeep Bavaliya
 
Code Profiling in Odoo 18 - Odoo 18 Slides
Code Profiling in Odoo 18 - Odoo 18 Slides
Celine George
 
Vitamin and Nutritional Deficiencies.pptx
Vitamin and Nutritional Deficiencies.pptx
Vishal Chanalia
 
NSUMD_M1 Library Orientation_June 11, 2025.pptx
NSUMD_M1 Library Orientation_June 11, 2025.pptx
Julie Sarpy
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
Gladiolous Cultivation practices by AKL.pdf
Gladiolous Cultivation practices by AKL.pdf
kushallamichhame
 
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT Kharagpur Quiz Club
 
Q1_ENGLISH_PPT_WEEK 1 power point grade 3 Quarter 1 week 1
Q1_ENGLISH_PPT_WEEK 1 power point grade 3 Quarter 1 week 1
jutaydeonne
 
This is why students from these 44 institutions have not received National Se...
This is why students from these 44 institutions have not received National Se...
Kweku Zurek
 
Q1_TLE 8_Week 1- Day 1 tools and equipment
Q1_TLE 8_Week 1- Day 1 tools and equipment
clairenotado3
 
A Visual Introduction to the Prophet Jeremiah
A Visual Introduction to the Prophet Jeremiah
Steve Thomason
 
How to Manage Different Customer Addresses in Odoo 18 Accounting
How to Manage Different Customer Addresses in Odoo 18 Accounting
Celine George
 
Ad

Creating GUI.pptx Gui graphical user interface

  • 2. Event Handling  An event can be defined as changing the state of an object or behavior by performing actions. Actions can be a button click, cursor movement, keypress through keyboard or page scrolling, etc.  The java.awt.event package can be used to provide various event classes.
  • 3. Event Handling • Foreground Events - Those events which require the direct interaction of user. They are generated as consequences of a person interacting with the graphical components in Graphical User Interface. For example, clicking on a button, moving the mouse, entering a character through keyboard, selecting an item from list, scrolling the page etc. • Background Events - Those events that require the interaction of end user are known as background events. Operating system interrupts, hardware or software failure, timer expires, an operation completion are the example of background events.
  • 4. Event Handling  Event Handling is the mechanism that controls the event and decides what should happen if an event occurs. This mechanism have the code which is known as event handler that is executed when an event occurs. Java Uses the Delegation Event Model to handle the events. This model defines the standard mechanism to generate and handle the events. Let's have a brief introduction to this model.  The Delegation Event Model has the following key participants namely: • Source - The source is an object on which event occurs. Source is responsible for providing information of the occurred event to it's handler. Java provide as with classes for source object. • Listener - It is also known as event handler. Listener is responsible for generating response to an event. From java implementation point of view the listener is also an object. Listener waits until it receives an event. Once the event is received , the listener process the event an then returns.
  • 5. Event Handling  Steps involved in event handling • The User clicks the button and the event is generated. • Now the object of concerned event class is created automatically and information about the source and the event get populated with in same object. • Event object is forwarded to the method of registered listener class. • the method is now get executed and returns.  Points to remember about listener • In order to design a listener class we have to develop some listener interfaces. These Listener interfaces forecast some public abstract callback methods which must be implemented by the listener class. • If you do not implement the any if the predefined interfaces then your class can not act as a listener class for a source object.
  • 6. Layout Managers  The LayoutManagers are used to arrange components in a particular manner. The Java LayoutManagers facilitates us to control the positioning and size of the components in GUI forms. LayoutManager is an interface that is implemented by all the classes of layout managers.  There are the following classes that represent the layout managers: 1. java.awt.BorderLayout 2. java.awt.FlowLayout 3. java.awt.GridLayout 4. java.awt.CardLayout 5. java.awt.GridBagLayout 6. javax.swing.BoxLayout 7. javax.swing.GroupLayout 8. javax.swing.ScrollPaneLayout 9. javax.swing.SpringLayout etc.
  • 7. Border Layout  The BorderLayout is used to arrange the components in five regions: north, south, east, west, and center. Each region (area) may contain one component only. It is the default layout of a frame or window. The BorderLayout provides five constants for each region: 1. public static final int NORTH 2. public static final int SOUTH 3. public static final int EAST 4. public static final int WEST 5. public static final int CENTER
  • 8. Flow Layout  The Java FlowLayout class is used to arrange the components in a line, one after another (in a flow). It is the default layout of the applet or panel.  Fields of FlowLayout class 1. public static final int LEFT 2. public static final int RIGHT 3. public static final int CENTER 4. public static final int LEADING 5. public static final int TRAILING
  • 9. Grid Layout  The Java GridLayout class is used to arrange the components in a rectangular grid. One component is displayed in each rectangle.  Constructors of GridLayout class 1. GridLayout(): creates a grid layout with one column per component in a row. 2. GridLayout(int rows, int columns): creates a grid layout with the given rows and columns but no gaps between the components. 3. GridLayout(int rows, int columns, int hgap, int vgap): creates a grid layout with the given rows and columns along with given horizontal and vertical gaps.
  • 10. CREATING A SIMPLE WINDOW start by creating a new Java project in the IDE of your choice (such as NetBeans or Eclipse). Create a new Java class and name it something like "MyWindow". In the class, import the necessary AWT classes using the import statement: we usually use import java.awt.* ( This library is typically called awt wild card library because it includes all the basic imports we need )  Create a new Frame object and set its properties, such as size, location, and background color: In AWT, a Frame is used to create a top-level window. A Frame is a subclass of Window and provides a title bar, borders, and the standard window management functionality of a typical application window, such as minimizing, maximizing, closing, and resizing. Therefore, a Frame provides a convenient way to create a window in AWT.
  • 11. Common Methods  setBounds(int x, int y, int width, int height): This method is used to set the size and position of a component within its container. It takes four parameters: x: The x-coordinate of the top-left corner of the component's bounding box. y: The y-coordinate of the top-left corner of the component's bounding box. width: The width of the component. height: The height of the component. For example, nameLabel.setBounds(50, 50, 60, 20); sets the bounds of the "Name" label component with its top-left corner at coordinates (50, 50) in the container, with a width of 60 pixels and a height of 20 pixels.  setSize(int width, int height): This method is used to set the size of a window or component. It takes two parameters: width: The width of the window or component. height: The height of the window or component. For example, frame.setSize(400, 300); sets the size of the frame to be 400 pixels wide and 300 pixels tall.
  • 12. Common Methods  setTitle(String title): This method is used to set the title of a window or frame. It takes one parameter: title: The title to be set for the window or frame. For example, frame.setTitle("Frame with Components"); sets the title of the frame to "Frame with Components".  setVisible(boolean visible): This method is used to set the visibility of a window or frame. It takes one parameter: visible: A boolean value indicating whether the window or frame should be visible (true) or invisible (false). For example, frame.setVisible(true); makes the frame visible on the screen.
  • 13. public class MyWindow extends Frame { public MyWindow() { // Set the title of the window setTitle("My AWT Window"); // Set the size of the window setSize(500, 300); // Set the location of the window on the screen setLocation(100, 100); // Set the background color of the window setBackground(Color.WHITE); // Make the window visible setVisible(true); } }
  • 14.  In the main method of the class, create an instance of the MyWindow class: public static void main(String[] args) { MyWindow myWindow = new MyWindow(); }
  • 15. ADDING A LABEL First, create a new instance of the Label class and set its text using the setText method. Next, set the position and size of the label using the setBounds method. Finally, add the label to the frame using the add method. Label label = new Label("Hello World!"); label.setBounds(50, 50, 100, 20); add(label);
  • 16. ADDING A BUTTON Create an instance of Button class: Button btn = new Button("Click me"); Here, we have created an instance of the Button class with the label "Click me". Set the size and position of the button using the setBounds() method: btn.setBounds(50, 80, 100, 20); Here, we have set the size of the button to 100x20 and positioned it at (50, 80) on the window. Add the button to the window using the add() method: add(btn); Here, we have added the button to the window.
  • 17. ADDING A PANEL  The Frame class is a top-level container, but it is not suitable for adding components directly. Instead, we need to create a Panel and add the components to it, and then add the panel to the frame.  So, in order to add a button to the window created, we first need to create a panel, add the button to it, and then add the panel to the frame. Here's an example code to add a button to the window: Panel panel = new Panel(); panel.setLayout(null); panel.add(button); This code creates a new panel, sets its layout to null (so we can specify the exact position and size of the button), creates a new button, sets its position and size, adds the button to the panel, adds the panel to the frame, and finally sets the frame to be visible.
  • 18. Add functionality to any specific button  To add functionality to a specific button in AWT, you need to perform the following steps: 1. Create an object of the Button class and add it to the Frame or Panel. 2. Implement the ActionListener interface and override the actionPerformed() method to specify the action to be performed when the button is clicked. 3. Add an ActionListener to the Button object using the addActionListener() method.
  • 19. // Create the button myButton = new Button("Click Me!"); // Add an ActionListener to the button myButton.addActionListener(this); // Add the button to the Frame add(myButton); public void actionPerformed(ActionEvent e) { // Perform the action when the button is clicked System.out.println("Button clicked!"); }
  • 20. ACTION LISTNER  In Java, an ActionListener is an interface that is used to handle events triggered by user actions such as button clicks, menu selections, and list selections. It provides a way for a program to register itself as an event listener for certain types of events and then respond appropriately when those events occur.  An ActionListener typically consists of a single method called actionPerformed(). This method is called automatically by the Java Virtual Machine (JVM) when the event occurs. Inside the actionPerformed() method, you can define the actions that should be taken in response to the event.  To use ActionListener in a program, you need to create an instance of a class that implements the ActionListener interface and then register that instance as an event listener for the component that generates the events. For example, if you want to handle button clicks, you need to create an instance of the ActionListener class and then register it with the button using the addButtonListener() method.
  • 21. ACTION LISTNER In AWT (Abstract Window Toolkit) in Java, the ActionListener interface is commonly used for handling action events. The ActionListener is part of the java.awt.event package. This interface is used to respond to events triggered by GUI components, such as buttons. Here's a brief explanation of ActionListener: ActionListener: Interface: java.awt.event.ActionListener Methods: void actionPerformed(ActionEvent e): This method is invoked when an action occurs. The code to be executed when an action (e.g., button click) happens should be written within this method.

Editor's Notes

  • #7: In Java AWT, you typically specify the layout manager for a container, and then the layout manager takes care of managing the layout of the components within that container based on the rules defined by the layout manager. When you add components to a container, the layout manager you've set for that container will determine how those components are positioned and sized within it. You don't need to manually calculate or specify the exact position and size of each component; the layout manager handles that for you based on its layout algorithm. So, while you do need to specify which layout manager to use for a container, you don't need to define the layout yourself in terms of specific coordinates or sizes. Instead, you rely on the layout manager to manage the layout according to its predefined rules. This allows for more flexible and maintainable GUI code, as the layout can adapt dynamically to changes in the container's size or the components it contains.
  • #10: Frame is the subclass of window. It
  • #13: SetTitle method is used to give title to the window we are creating setSize ( 500,300) -> 1st parameter determines the width and 2nd determines the length SetLocation (100,100) -> These parameters are x, y coordinates setVisible (true) -. This mehod is compulsory to show all the components on the screem after adding it into the window. If we do not use, nothing will appear
  • #15: After creating a lable we need to use the add method to add that label on the frame setBounds =It takes four arguments: x, y, width, and height. The x and y arguments specify the position of the component relative to the top-left corner of its container, and the width and height arguments specify the size of the component. There are different methods available to adding a label
  • #16: The Frame class is a top-level container, but it is not suitable for adding components directly. Instead, we need to create a Panel and add the components to it, and then add the panel to the frame.
  • #19: ActionEvent is an event that is generated when a button is clicked. When the event is generated, it calls the actionPerformed() method which contains the code that should be executed when the event is triggered.