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.

More Related Content

PPT
Java_gui_with_AWT_and_its_components.ppt
JyothiAmpally
 
PPT
Basic of Abstract Window Toolkit(AWT) in Java
suraj pandey
 
PPTX
javaprogramming framework-ppt frame.pptx
DrDGayathriDevi
 
PPT
Swing and AWT in java
Adil Mehmoood
 
PDF
Swingpre 150616004959-lva1-app6892
renuka gavli
 
PPTX
AbstractWindowToolkit33333333333333.pptx
geetav5
 
PDF
Session 9_AWT in java with all demonstrations.pdf
tabbu23
 
Java_gui_with_AWT_and_its_components.ppt
JyothiAmpally
 
Basic of Abstract Window Toolkit(AWT) in Java
suraj pandey
 
javaprogramming framework-ppt frame.pptx
DrDGayathriDevi
 
Swing and AWT in java
Adil Mehmoood
 
Swingpre 150616004959-lva1-app6892
renuka gavli
 
AbstractWindowToolkit33333333333333.pptx
geetav5
 
Session 9_AWT in java with all demonstrations.pdf
tabbu23
 

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

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

Recently uploaded (20)

PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
PPTX
Trends in pediatric nursing .pptx
AneetaSharma15
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PPTX
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
PDF
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
PDF
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PDF
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PPTX
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
PPTX
Autodock-for-Beginners by Rahul D Jawarkar.pptx
Rahul Jawarkar
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
Trends in pediatric nursing .pptx
AneetaSharma15
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
Autodock-for-Beginners by Rahul D Jawarkar.pptx
Rahul Jawarkar
 
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.