SlideShare a Scribd company logo
Java AWT
Java AWT
• Java AWT (Abstract Windowing Toolkit) is an API
to develop GUI or window-based application in
java.
• Java AWT components are platform-dependent
i.e. components are displayed according to the
view of operating system.
• AWT is heavyweight i.e. its components uses the
resources of system.
• The java.awt package provides classes for AWT
api such as TextField, Label, TextArea,
RadioButton, CheckBox, Choice, List etc.
Java AWT Hierarchy
• The hierarchy of Java AWT classes are given below.
• Container
• The Container is a component in AWT that can contain another
components like buttons, textfields, labels etc. The classes that extends
Container class are known as container such as Frame, Dialog and Panel.
• Window
• The window is the container that have no borders and menu bars. You
must use frame, dialog or another window for creating a window.
• Panel
• The Panel is the container that doesn't contain title bar and menu bars. It
can have other components like button, textfield etc.
• Frame
• The Frame is the container that contain title bar and can have menu bars.
It can have other components like button, textfield etc.
Useful Methods of Component class
Method Description
public void add(Component c) inserts a component on this
component.
public void setSize(int width,int height) sets the size (width and height) of the
component.
public void setLayout(LayoutManager
m)
defines the layout manager for the
component.
public void setVisible(boolean status) changes the visibility of the component,
by default false.
Java AWT Example
• To create simple awt example, you need a
frame. There are two ways to create a frame
in AWT.
• By extending Frame class (inheritance)
• By creating the object of Frame class
(association)
Simple example of AWT by inheritance
import java.awt.*;
class First extends Frame{
First(){
Button b=new Button("click me");
b.setBounds(30,100,80,30);// setting button position
add(b);//adding button into frame
setSize(300,300);//frame size 300 width and 300 height
setLayout(null);//no layout manager
setVisible(true);//now frame will be visible, by defaul
t not visible
}
public static void main(String args[]){
First f=new First();
}}
Simple example of AWT by association
import java.awt.*;
class First2{
First2(){
Frame f=new Frame();
Button b=new Button("click me");
b.setBounds(30,50,80,30);
f.add(b);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[]){
First2 f=new First2();
}}
Event and Listener (Java Event
Handling)
• Changing the state of an object is known as an
event. For example, click on button, dragging
mouse etc.
• The java.awt.event package provides many
event classes and Listener interfaces for event
handling.
Event classes and Listener interfaces:
Event Classes Listener Interfaces
ActionEvent ActionListener
MouseEvent MouseListener and MouseMotionListener
MouseWheelEvent MouseWheelListener
KeyEvent KeyListener
ItemEvent ItemListener
TextEvent TextListener
AdjustmentEvent AdjustmentListener
WindowEvent WindowListener
ComponentEvent ComponentListener
ContainerEvent ContainerListener
FocusEvent FocusListener
Steps to perform Event Handling
• Following steps are required to perform event
handling:
1. Implement the Listener interface and
overrides its methods
2. Register the component with the Listener
For registering the component with the Listener, many
classes provide the registration methods. For example:
– Button
• public void addActionListener(ActionListener a){}
– MenuItem
• public void addActionListener(ActionListener a){}
– TextField
• public void addActionListener(ActionListener a){}
• public void addTextListener(TextListener a){}
– TextArea
• public void addTextListener(TextListener a){}
– Checkbox
• public void addItemListener(ItemListener a){}
– Choice
• public void addItemListener(ItemListener a){}
– List
• public void addActionListener(ActionListener a){}
• public void addItemListener(ItemListener a){}
EventHandling Codes:
• We can put the event handling code into one
of the following places:
1. Same class
2. Other class
3. Anonymous class
Example of event handling within class:
import java.awt.*;
import java.awt.event.*;
class AEvent extends Frame implem
ents ActionListener{
TextField tf;
AEvent(){
tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("click me");
b.setBounds(100,120,80,30);
b.addActionListener(this);
add(b);add(tf);
setSize(300,300);
setLayout(null);
setVisible(true); }
public void actionPerforme
d(ActionEvent e){
tf.setText("Welcome");
}
public static void main(St
ring args[]){
new AEvent();
}
}
Example of event handling by Outer class:
import java.awt.*;
import java.awt.even
t.*;
class AEvent2 extend
s Frame{
TextField tf;
AEvent2(){
tf=new TextField();
tf.setBounds(60,50,1
70,20);
Button b=new Button(
"click me");
b.setBounds(100,120,80,30);
Outer o=new Outer(this);
b.addActionListener(o);//passi
ng outer class instance
add(b);add(tf);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public static void main(String
args[]){
new AEvent2(); }
}
Cont…
import java.awt.event.*;
class Outer implements ActionListener{
AEvent2 obj;
Outer(AEvent2 obj){
this.obj=obj;
}
public void actionPerformed(ActionEvent e)
{
obj.tf.setText("welcome");
}
}
Example of event handling by Annonymous class:
import java.awt.*;
import java.awt.event
.*;
class AEvent3 extends
Frame{
TextField tf;
AEvent3(){
tf=new TextField();
tf.setBounds(60,50,17
0,20);
Button b=new Button("
click me");
b.setBounds(50,120,80
,30);
b.addActionListener(new Acti
onListener(){
public void actionPerformed(
){
tf.setText("hello");
}
});
add(b);add(tf);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public static void main(Stri
ng args[]){
new AEvent3();
}
}
Ad

Recommended

Java chapter 7
Java chapter 7
Abdii Rashid
 
Awt
Awt
Rakesh Patil
 
Awt controls ppt
Awt controls ppt
soumyaharitha
 
Java awt tutorial javatpoint
Java awt tutorial javatpoint
Ricardo Garcia
 
Applet in java
Applet in java
Rakesh Mittal
 
tL19 awt
tL19 awt
teach4uin
 
JAVA AWT
JAVA AWT
shanmuga rajan
 
Windows Programming with AWT
Windows Programming with AWT
backdoor
 
GUI Programming In Java
GUI Programming In Java
yht4ever
 
GUI Programming with Java
GUI Programming with Java
Jussi Pohjolainen
 
AWT information
AWT information
Unit Nexus Pvt. Ltd.
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
Elizabeth alexander
 
AWT
AWT
ravi9kumar
 
28 awt
28 awt
Prachi Vijh
 
Java swing
Java swing
Arati Gadgil
 
Basic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in Java
suraj pandey
 
Dr. Rajeshree Khande :Introduction to Java AWT
Dr. Rajeshree Khande :Introduction to Java AWT
DrRajeshreeKhande
 
25 awt
25 awt
degestive
 
Client Side Programming with Applet
Client Side Programming with Applet
backdoor
 
Awt components
Awt components
Balwinder Kumar
 
GUI components in Java
GUI components in Java
kirupasuchi1996
 
Awt and swing in java
Awt and swing in java
Shehrevar Davierwala
 
Layout manager
Layout manager
Shree M.L.Kakadiya MCA mahila college, Amreli
 
Swing
Swing
Jaydeep Viradiya
 
Java swing
Java swing
Apurbo Datta
 
Swing and AWT in java
Swing and AWT in java
Adil Mehmoood
 
java swing
java swing
Waheed Warraich
 
Java- GUI- Mazenet solution
Java- GUI- Mazenet solution
Mazenetsolution
 
java- Abstract Window toolkit
java- Abstract Window toolkit
Jayant Dalvi
 
javaprogramming framework-ppt frame.pptx
javaprogramming framework-ppt frame.pptx
DrDGayathriDevi
 

More Related Content

What's hot (20)

GUI Programming In Java
GUI Programming In Java
yht4ever
 
GUI Programming with Java
GUI Programming with Java
Jussi Pohjolainen
 
AWT information
AWT information
Unit Nexus Pvt. Ltd.
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
Elizabeth alexander
 
AWT
AWT
ravi9kumar
 
28 awt
28 awt
Prachi Vijh
 
Java swing
Java swing
Arati Gadgil
 
Basic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in Java
suraj pandey
 
Dr. Rajeshree Khande :Introduction to Java AWT
Dr. Rajeshree Khande :Introduction to Java AWT
DrRajeshreeKhande
 
25 awt
25 awt
degestive
 
Client Side Programming with Applet
Client Side Programming with Applet
backdoor
 
Awt components
Awt components
Balwinder Kumar
 
GUI components in Java
GUI components in Java
kirupasuchi1996
 
Awt and swing in java
Awt and swing in java
Shehrevar Davierwala
 
Layout manager
Layout manager
Shree M.L.Kakadiya MCA mahila college, Amreli
 
Swing
Swing
Jaydeep Viradiya
 
Java swing
Java swing
Apurbo Datta
 
Swing and AWT in java
Swing and AWT in java
Adil Mehmoood
 
java swing
java swing
Waheed Warraich
 
Java- GUI- Mazenet solution
Java- GUI- Mazenet solution
Mazenetsolution
 

Similar to object oriented programming examples (20)

java- Abstract Window toolkit
java- Abstract Window toolkit
Jayant Dalvi
 
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
 
JAVA AWT presentation for awt key events.pptx
JAVA AWT presentation for awt key events.pptx
ShubhamNain11
 
Creating GUI.pptx Gui graphical user interface
Creating GUI.pptx Gui graphical user interface
pikachu02434
 
Awt event
Awt event
Vijay Kumar
 
AWT.pptx
AWT.pptx
FAHMIDAASEEZ1
 
Gu iintro(java)
Gu iintro(java)
Satish Verma
 
MODULE 5.pptx gui programming and applets
MODULE 5.pptx gui programming and applets
LIKITHLIKITH7
 
Unit 4_1.pptx JDBC AND GUI FOR CLIENT SERVER
Unit 4_1.pptx JDBC AND GUI FOR CLIENT SERVER
Salini P
 
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
 
The AWT and Swing
The AWT and Swing
adil raja
 
JAVA (UNIT 5)
JAVA (UNIT 5)
Dr. SURBHI SAROHA
 
Java_gui_with_AWT_and_its_components.ppt
Java_gui_with_AWT_and_its_components.ppt
JyothiAmpally
 
engineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.ppt
sharanyak0721
 
14a-gui.ppt
14a-gui.ppt
DrDGayathriDevi
 
Gui
Gui
Sardar Alam
 
PraveenKumar A T AWS
PraveenKumar A T AWS
Praveen Kumar
 
Java AWT Controls and methods, Listener classes
Java AWT Controls and methods, Listener classes
muthulakshmi279332
 
java- Abstract Window toolkit
java- Abstract Window toolkit
Jayant Dalvi
 
javaprogramming framework-ppt frame.pptx
javaprogramming framework-ppt frame.pptx
DrDGayathriDevi
 
JAVA AWT presentation for awt key events.pptx
JAVA AWT presentation for awt key events.pptx
ShubhamNain11
 
Creating GUI.pptx Gui graphical user interface
Creating GUI.pptx Gui graphical user interface
pikachu02434
 
MODULE 5.pptx gui programming and applets
MODULE 5.pptx gui programming and applets
LIKITHLIKITH7
 
Unit 4_1.pptx JDBC AND GUI FOR CLIENT SERVER
Unit 4_1.pptx JDBC AND GUI FOR CLIENT SERVER
Salini P
 
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
 
The AWT and Swing
The AWT and Swing
adil raja
 
Java_gui_with_AWT_and_its_components.ppt
Java_gui_with_AWT_and_its_components.ppt
JyothiAmpally
 
engineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.ppt
sharanyak0721
 
PraveenKumar A T AWS
PraveenKumar A T AWS
Praveen Kumar
 
Java AWT Controls and methods, Listener classes
Java AWT Controls and methods, Listener classes
muthulakshmi279332
 
Ad

More from Abdii Rashid (9)

Java chapter 6
Java chapter 6
Abdii Rashid
 
Java chapter 5
Java chapter 5
Abdii Rashid
 
Java chapter 4
Java chapter 4
Abdii Rashid
 
Java chapter 3
Java chapter 3
Abdii Rashid
 
Java chapter 2
Java chapter 2
Abdii Rashid
 
object oriented programming examples
object oriented programming examples
Abdii Rashid
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for print
Abdii Rashid
 
Chapter 7 graphs
Chapter 7 graphs
Abdii Rashid
 
Chapter 1 introduction haramaya
Chapter 1 introduction haramaya
Abdii Rashid
 
object oriented programming examples
object oriented programming examples
Abdii Rashid
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for print
Abdii Rashid
 
Chapter 1 introduction haramaya
Chapter 1 introduction haramaya
Abdii Rashid
 
Ad

Recently uploaded (20)

FSE-Journal-First-Automated code editing with search-generate-modify.pdf
FSE-Journal-First-Automated code editing with search-generate-modify.pdf
cl144
 
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
Structural Wonderers_new and ancient.pptx
Structural Wonderers_new and ancient.pptx
nikopapa113
 
retina_biometrics ruet rajshahi bangdesh.pptx
retina_biometrics ruet rajshahi bangdesh.pptx
MdRakibulIslam697135
 
Rapid Prototyping for XR: Lecture 6 - AI for Prototyping and Research Directi...
Rapid Prototyping for XR: Lecture 6 - AI for Prototyping and Research Directi...
Mark Billinghurst
 
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
محمد قصص فتوتة
 
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
resming1
 
How to Un-Obsolete Your Legacy Keypad Design
How to Un-Obsolete Your Legacy Keypad Design
Epec Engineered Technologies
 
Mobile database systems 20254545645.pptx
Mobile database systems 20254545645.pptx
herosh1968
 
輪読会資料_Miipher and Miipher2 .
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
Call For Papers - 17th International Conference on Wireless & Mobile Networks...
Call For Papers - 17th International Conference on Wireless & Mobile Networks...
hosseinihamid192023
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
CST413 KTU S7 CSE Machine Learning Clustering K Means Hierarchical Agglomerat...
CST413 KTU S7 CSE Machine Learning Clustering K Means Hierarchical Agglomerat...
resming1
 
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Mark Billinghurst
 
International Journal of Advanced Information Technology (IJAIT)
International Journal of Advanced Information Technology (IJAIT)
ijait
 
special_edition_using_visual_foxpro_6.pdf
special_edition_using_visual_foxpro_6.pdf
Shabista Imam
 
DESIGN OF REINFORCED CONCRETE ELEMENTS S
DESIGN OF REINFORCED CONCRETE ELEMENTS S
prabhusp8
 
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
moonsony54
 
Industrial internet of things IOT Week-3.pptx
Industrial internet of things IOT Week-3.pptx
KNaveenKumarECE
 
Proposal for folders structure division in projects.pdf
Proposal for folders structure division in projects.pdf
Mohamed Ahmed
 
FSE-Journal-First-Automated code editing with search-generate-modify.pdf
FSE-Journal-First-Automated code editing with search-generate-modify.pdf
cl144
 
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
Structural Wonderers_new and ancient.pptx
Structural Wonderers_new and ancient.pptx
nikopapa113
 
retina_biometrics ruet rajshahi bangdesh.pptx
retina_biometrics ruet rajshahi bangdesh.pptx
MdRakibulIslam697135
 
Rapid Prototyping for XR: Lecture 6 - AI for Prototyping and Research Directi...
Rapid Prototyping for XR: Lecture 6 - AI for Prototyping and Research Directi...
Mark Billinghurst
 
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
محمد قصص فتوتة
 
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
resming1
 
Mobile database systems 20254545645.pptx
Mobile database systems 20254545645.pptx
herosh1968
 
輪読会資料_Miipher and Miipher2 .
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
Call For Papers - 17th International Conference on Wireless & Mobile Networks...
Call For Papers - 17th International Conference on Wireless & Mobile Networks...
hosseinihamid192023
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
CST413 KTU S7 CSE Machine Learning Clustering K Means Hierarchical Agglomerat...
CST413 KTU S7 CSE Machine Learning Clustering K Means Hierarchical Agglomerat...
resming1
 
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Mark Billinghurst
 
International Journal of Advanced Information Technology (IJAIT)
International Journal of Advanced Information Technology (IJAIT)
ijait
 
special_edition_using_visual_foxpro_6.pdf
special_edition_using_visual_foxpro_6.pdf
Shabista Imam
 
DESIGN OF REINFORCED CONCRETE ELEMENTS S
DESIGN OF REINFORCED CONCRETE ELEMENTS S
prabhusp8
 
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
moonsony54
 
Industrial internet of things IOT Week-3.pptx
Industrial internet of things IOT Week-3.pptx
KNaveenKumarECE
 
Proposal for folders structure division in projects.pdf
Proposal for folders structure division in projects.pdf
Mohamed Ahmed
 

object oriented programming examples

  • 2. Java AWT • Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based application in java. • Java AWT components are platform-dependent i.e. components are displayed according to the view of operating system. • AWT is heavyweight i.e. its components uses the resources of system. • The java.awt package provides classes for AWT api such as TextField, Label, TextArea, RadioButton, CheckBox, Choice, List etc.
  • 3. Java AWT Hierarchy • The hierarchy of Java AWT classes are given below.
  • 4. • Container • The Container is a component in AWT that can contain another components like buttons, textfields, labels etc. The classes that extends Container class are known as container such as Frame, Dialog and Panel. • Window • The window is the container that have no borders and menu bars. You must use frame, dialog or another window for creating a window. • Panel • The Panel is the container that doesn't contain title bar and menu bars. It can have other components like button, textfield etc. • Frame • The Frame is the container that contain title bar and can have menu bars. It can have other components like button, textfield etc.
  • 5. Useful Methods of Component class Method Description public void add(Component c) inserts a component on this component. public void setSize(int width,int height) sets the size (width and height) of the component. public void setLayout(LayoutManager m) defines the layout manager for the component. public void setVisible(boolean status) changes the visibility of the component, by default false.
  • 6. Java AWT Example • To create simple awt example, you need a frame. There are two ways to create a frame in AWT. • By extending Frame class (inheritance) • By creating the object of Frame class (association)
  • 7. Simple example of AWT by inheritance import java.awt.*; class First extends Frame{ First(){ Button b=new Button("click me"); b.setBounds(30,100,80,30);// setting button position add(b);//adding button into frame setSize(300,300);//frame size 300 width and 300 height setLayout(null);//no layout manager setVisible(true);//now frame will be visible, by defaul t not visible } public static void main(String args[]){ First f=new First(); }}
  • 8. Simple example of AWT by association import java.awt.*; class First2{ First2(){ Frame f=new Frame(); Button b=new Button("click me"); b.setBounds(30,50,80,30); f.add(b); f.setSize(300,300); f.setLayout(null); f.setVisible(true); } public static void main(String args[]){ First2 f=new First2(); }}
  • 9. Event and Listener (Java Event Handling) • Changing the state of an object is known as an event. For example, click on button, dragging mouse etc. • The java.awt.event package provides many event classes and Listener interfaces for event handling.
  • 10. Event classes and Listener interfaces: Event Classes Listener Interfaces ActionEvent ActionListener MouseEvent MouseListener and MouseMotionListener MouseWheelEvent MouseWheelListener KeyEvent KeyListener ItemEvent ItemListener TextEvent TextListener AdjustmentEvent AdjustmentListener WindowEvent WindowListener ComponentEvent ComponentListener ContainerEvent ContainerListener FocusEvent FocusListener
  • 11. Steps to perform Event Handling • Following steps are required to perform event handling: 1. Implement the Listener interface and overrides its methods 2. Register the component with the Listener
  • 12. For registering the component with the Listener, many classes provide the registration methods. For example: – Button • public void addActionListener(ActionListener a){} – MenuItem • public void addActionListener(ActionListener a){} – TextField • public void addActionListener(ActionListener a){} • public void addTextListener(TextListener a){} – TextArea • public void addTextListener(TextListener a){} – Checkbox • public void addItemListener(ItemListener a){} – Choice • public void addItemListener(ItemListener a){} – List • public void addActionListener(ActionListener a){} • public void addItemListener(ItemListener a){}
  • 13. EventHandling Codes: • We can put the event handling code into one of the following places: 1. Same class 2. Other class 3. Anonymous class
  • 14. Example of event handling within class: import java.awt.*; import java.awt.event.*; class AEvent extends Frame implem ents ActionListener{ TextField tf; AEvent(){ tf=new TextField(); tf.setBounds(60,50,170,20); Button b=new Button("click me"); b.setBounds(100,120,80,30); b.addActionListener(this); add(b);add(tf); setSize(300,300); setLayout(null); setVisible(true); } public void actionPerforme d(ActionEvent e){ tf.setText("Welcome"); } public static void main(St ring args[]){ new AEvent(); } }
  • 15. Example of event handling by Outer class: import java.awt.*; import java.awt.even t.*; class AEvent2 extend s Frame{ TextField tf; AEvent2(){ tf=new TextField(); tf.setBounds(60,50,1 70,20); Button b=new Button( "click me"); b.setBounds(100,120,80,30); Outer o=new Outer(this); b.addActionListener(o);//passi ng outer class instance add(b);add(tf); setSize(300,300); setLayout(null); setVisible(true); } public static void main(String args[]){ new AEvent2(); } }
  • 16. Cont… import java.awt.event.*; class Outer implements ActionListener{ AEvent2 obj; Outer(AEvent2 obj){ this.obj=obj; } public void actionPerformed(ActionEvent e) { obj.tf.setText("welcome"); } }
  • 17. Example of event handling by Annonymous class: import java.awt.*; import java.awt.event .*; class AEvent3 extends Frame{ TextField tf; AEvent3(){ tf=new TextField(); tf.setBounds(60,50,17 0,20); Button b=new Button(" click me"); b.setBounds(50,120,80 ,30); b.addActionListener(new Acti onListener(){ public void actionPerformed( ){ tf.setText("hello"); } }); add(b);add(tf); setSize(300,300); setLayout(null); setVisible(true); } public static void main(Stri ng args[]){ new AEvent3(); } }