SlideShare a Scribd company logo
GUI(Graphical User Interface)
Rishi Ram Khanal
BIM (TU)
Introduction to GUI
• Graphical user interface is a type of user interface
that allows users to interact with the screen using
graphical components(Visual Indicators) rather
than text commands.
• There are two sets of Java APIs(Application
Programming Interface) for graphics
Programming:
1.AWT(Abstract Windowing Toolkit)
2.Swing
AWT
• It consist of 12 packages
• Only 2 packages-java.awt and java.awt.event
are commonly used
• The java.awt package contains the core AWT
graphics classes
• GUI component classes(such as Button, Textfield ,label)
• GUI container classes(frame ,panel,Dialog)
• Layout mananger(flowlayout,Borderlayout,Gridlayout)
• Custom graphic classes(Graphic,Color,Font)
AWT Events
• The java .awt.event package supports event
handling
• Event classes (ActionEvent,
MouseEvent,KeyEvent and WindowEvent)
• Event Listener Interfaces(Such as
ActionListener,Mouselistener,
WindowsListener)
Container
• A frame is the top –level container of an AWT
GUI program
• A panel is a rectangular area(or portion) used
to group related GUI components
• In a GUI program , a component must be kept
in a container
• Every container has a method called
add(Component c)
GUI (graphical user interface)
APPLET
• An applet is a java program that runs in a Web browser
• Applet is a container class like Frame
• An applet is a java class that extends the
java.applet.Applet class
• Applets are designed to be embedded within an HTML
page
• When a user views an Html page that contains an
applet ,the code for the applet is downloaded to the
user’s machine
• A JVM is required to view an applet
PRATICAL
• How to make an applet Program?
• How a programmer can compile and run an
applet code?
• How applet can be embedded in the HTML
code?
LIFE CYCLE OF APPLET
• JVM on the user’s machine creates an instance of
the applet class and invokes various methods
during the applet’s life time
• Four method in the Applet class give you the
framework on which you can build any applet
Application
• Init()
• Start()
• Stop()
• Destroy()
• Paint()(important function but not a part of Lifecycle)
How to set Components in Applet
• Component
• Button
• Textfield
• Label
• CheckBox
• List
• We need to design GUI so that user can
interact with the applet
• We have to place component on the applet
container , to meet this requirement
For pratical
• Basic Skeleton of an applet
• Creating Component reference variables
• Initialize Components
• Adding Components to the applet
• Setting layout
Event Handling in an Applet In java
• Event
Changing the state of an object is knows
as Event. For example click on button,
dragging mouse, minimizing window etc
The java.awt.event package provides many
event classes and Listener interfaces for event
handling
Event handling is to make java code ready to
respond any particular event
Feeling of a button
Hey , I am button
class object. user can
click me, but I don’t
know what to run
when he does that
Feeling of the programmer(you)
I am the programmer , I have to write code that
should be executed on click of a button
Public void someFunction()
{
write some code
}
Struggling phase:
But do my function get to know that
button has just clicked?
Two Ends Of Story
Hey programmer , I have method to
register to register your code with me
Ok that means
button.addActionListener();
Communication can solve the issue
Yes , but you have to pass an object to
function
Button.addActionListener(here?);
But object of which class?
See prototype defined in Button class
Public void addActionListener(ActionListener,
actionlistener)
But ActionListener ia an interface, which I have to
pass an object of class which implements
ActionLIstener.
Button.addActionListener(object);
Ok, then I have to make a class which implements
ActionListener
Class Myhandler implements ActionListener
{
Public void someFunction (ActionEvent e)
{
write some code
}
}
Button.addActionListener(new MyHandler());
This is the way I can register my
handler with my Button object
Now I have an object
of Myhandler
referenced by
actionListener(a
reference variable of
ActionListener)
Parameter Passing in an Applet
<param> tag
• Parameter are passed to applets in Name=Value
pairs in <param> tags betweeen the opening and
closing applet tags
• Inside the applet , you read the values passed
through the param tags with the getParameter()
method of the java.applet.Applet class
Import java.applet.Applet;
Import java.awt.*;
/*<applet code=“Myapplet” width=“300” height=“500”
<param name=“saroj” value=“paras”></param></applet>
Public class Myapplet extends Applet
{
String message=“hello”;
Public void paint(Graphics s)
{
String s1=this.getParameter(“saroj”);
if(s1==null)
s1=message;
s.drawString(s1,150,150);
}
}
Introduction to AWT
• Java Awt(Abstract Windowing Toolkit) is an
API to develop GUI or window-based
application in java.
• AWT classes
• GUI component classes(such as Button, Textfield ,label)
• GUI container classes(frame ,panel,DialogBoxes)
• Layout mananger(flowlayout,Borderlayout,Gridlayout)
• Custom graphic classes(Graphic,Color,Font)
• Event Handling
classes(AWTEvent,AwteEventMultiCaster)
Hierarchy of java AWT classes
Frame
• The Frame is the container that contain title bar
and can have menu bars. It can have other
components like button, textfield etc.
• The window is the container that have no borders
and menu bars. You must use frame, dialog or
another window for creating a window.
• The Panel is the container that doesn't contain
title bar and menu bars. It can have other
components like button, textfield etc.
Working with Frame Window
• Setting the window’s Dimensions
Void setSize(init newWidth, init newHeight)
void setSize(Dimension newSize)
getSize() method is used to obtain current size
of a window.
• Hiding and showing a window
void setVisible(boolean visibleFlag)
Setting a window’s Title
void setTitle(String newTitle)
Example of AWT frame
Import java.awt.*;
Class MyFrame extends Frame
{
MyFrame()
{
setTitle(“Myfirst Frame”);
setSize(200,200);
Button b=new Button(“click Me”);
setLayout(null);
b.setBounds(50,50,50,50);
add(b);
setVisible(true);
}
}
Practical
Ad

Recommended

Swing and Graphical User Interface in Java
Swing and Graphical User Interface in Java
babak danyal
 
JAVA GUI PART III
JAVA GUI PART III
OXUS 20
 
GUI Programming In Java
GUI Programming In Java
yht4ever
 
Gui
Gui
Sardar Alam
 
Java- GUI- Mazenet solution
Java- GUI- Mazenet solution
Mazenetsolution
 
AWT
AWT
ravi9kumar
 
28 awt
28 awt
Prachi Vijh
 
Unit 6 Java
Unit 6 Java
arnold 7490
 
GUI Programming with Java
GUI Programming with Java
Jussi Pohjolainen
 
Awt
Awt
Rakesh Patil
 
Event Handling in Java
Event Handling in Java
Ayesha Kanwal
 
Event Handling in java
Event Handling in java
Google
 
Advance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handling
Payal Dungarwal
 
Event handling
Event handling
Anand Grewal
 
Event Handling in JAVA
Event Handling in JAVA
Srajan Shukla
 
GUI components in Java
GUI components in Java
kirupasuchi1996
 
Event handling
Event handling
Shree M.L.Kakadiya MCA mahila college, Amreli
 
GUI Programming in JAVA (Using Netbeans) - A Review
GUI Programming in JAVA (Using Netbeans) - A Review
Fernando Torres
 
Java: GUI
Java: GUI
Tareq Hasan
 
Swing
Swing
Jaydeep Viradiya
 
Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1
PRN USM
 
Java awt tutorial javatpoint
Java awt tutorial javatpoint
Ricardo Garcia
 
Basic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in Java
suraj pandey
 
java swing
java swing
Waheed Warraich
 
Event handling
Event handling
Ravi_Kant_Sahu
 
tL19 awt
tL19 awt
teach4uin
 
Dr. Rajeshree Khande :Introduction to Java AWT
Dr. Rajeshree Khande :Introduction to Java AWT
DrRajeshreeKhande
 
GUI programming
GUI programming
Vineeta Garg
 
25 awt
25 awt
degestive
 
GUI.pdf
GUI.pdf
Poornima E.G.
 

More Related Content

What's hot (20)

GUI Programming with Java
GUI Programming with Java
Jussi Pohjolainen
 
Awt
Awt
Rakesh Patil
 
Event Handling in Java
Event Handling in Java
Ayesha Kanwal
 
Event Handling in java
Event Handling in java
Google
 
Advance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handling
Payal Dungarwal
 
Event handling
Event handling
Anand Grewal
 
Event Handling in JAVA
Event Handling in JAVA
Srajan Shukla
 
GUI components in Java
GUI components in Java
kirupasuchi1996
 
Event handling
Event handling
Shree M.L.Kakadiya MCA mahila college, Amreli
 
GUI Programming in JAVA (Using Netbeans) - A Review
GUI Programming in JAVA (Using Netbeans) - A Review
Fernando Torres
 
Java: GUI
Java: GUI
Tareq Hasan
 
Swing
Swing
Jaydeep Viradiya
 
Graphical User Interface (GUI) - 1
Graphical User Interface (GUI) - 1
PRN USM
 
Java awt tutorial javatpoint
Java awt tutorial javatpoint
Ricardo Garcia
 
Basic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in Java
suraj pandey
 
java swing
java swing
Waheed Warraich
 
Event handling
Event handling
Ravi_Kant_Sahu
 
tL19 awt
tL19 awt
teach4uin
 
Dr. Rajeshree Khande :Introduction to Java AWT
Dr. Rajeshree Khande :Introduction to Java AWT
DrRajeshreeKhande
 
GUI programming
GUI programming
Vineeta Garg
 

Similar to GUI (graphical user interface) (20)

25 awt
25 awt
degestive
 
GUI.pdf
GUI.pdf
Poornima E.G.
 
java Unit4 chapter1 applets
java Unit4 chapter1 applets
raksharao
 
Java AWT Controls and methods, Listener classes
Java AWT Controls and methods, Listener classes
muthulakshmi279332
 
appletcomponentsjbjxbsjkcsjkcjmsc jc.ppt
appletcomponentsjbjxbsjkcsjkcjmsc jc.ppt
Vijay Bhaskar Thatty
 
javaprogramming framework-ppt frame.pptx
javaprogramming framework-ppt frame.pptx
DrDGayathriDevi
 
PraveenKumar A T AWS
PraveenKumar A T AWS
Praveen Kumar
 
Java session13
Java session13
Jigarthacker
 
java- Abstract Window toolkit
java- Abstract Window toolkit
Jayant Dalvi
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
Elizabeth alexander
 
Creating GUI.pptx Gui graphical user interface
Creating GUI.pptx Gui graphical user interface
pikachu02434
 
15a gui
15a gui
Program in Interdisciplinary Computing
 
JAVA AWT presentation for awt key events.pptx
JAVA AWT presentation for awt key events.pptx
ShubhamNain11
 
Lecture8 oopj
Lecture8 oopj
Dhairya Joshi
 
Unit 1- awt(Abstract Window Toolkit) .ppt
Unit 1- awt(Abstract Window Toolkit) .ppt
Deepgaichor1
 
GUI JAVA PROG ~hmftj
GUI JAVA PROG ~hmftj
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies
 
Swing and AWT in java
Swing and AWT in java
Adil Mehmoood
 
Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892
renuka gavli
 
fdtrdrtttxxxtrtrctctrttrdredrerrrrrrawt.ppt
fdtrdrtttxxxtrtrctctrttrdredrerrrrrrawt.ppt
havalneha2121
 
awdrdtfffyfyfyfyfyfyfyfyfyfyfyfyyfyt.ppt
awdrdtfffyfyfyfyfyfyfyfyfyfyfyfyyfyt.ppt
SulbhaBhivsane
 
java Unit4 chapter1 applets
java Unit4 chapter1 applets
raksharao
 
Java AWT Controls and methods, Listener classes
Java AWT Controls and methods, Listener classes
muthulakshmi279332
 
appletcomponentsjbjxbsjkcsjkcjmsc jc.ppt
appletcomponentsjbjxbsjkcsjkcjmsc jc.ppt
Vijay Bhaskar Thatty
 
javaprogramming framework-ppt frame.pptx
javaprogramming framework-ppt frame.pptx
DrDGayathriDevi
 
PraveenKumar A T AWS
PraveenKumar A T AWS
Praveen Kumar
 
java- Abstract Window toolkit
java- Abstract Window toolkit
Jayant Dalvi
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
Elizabeth alexander
 
Creating GUI.pptx Gui graphical user interface
Creating GUI.pptx Gui graphical user interface
pikachu02434
 
JAVA AWT presentation for awt key events.pptx
JAVA AWT presentation for awt key events.pptx
ShubhamNain11
 
Unit 1- awt(Abstract Window Toolkit) .ppt
Unit 1- awt(Abstract Window Toolkit) .ppt
Deepgaichor1
 
Swing and AWT in java
Swing and AWT in java
Adil Mehmoood
 
Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892
renuka gavli
 
fdtrdrtttxxxtrtrctctrttrdredrerrrrrrawt.ppt
fdtrdrtttxxxtrtrctctrttrdredrerrrrrrawt.ppt
havalneha2121
 
awdrdtfffyfyfyfyfyfyfyfyfyfyfyfyyfyt.ppt
awdrdtfffyfyfyfyfyfyfyfyfyfyfyfyyfyt.ppt
SulbhaBhivsane
 
Ad

More from rishi ram khanal (20)

Measurement of gdp under product method
Measurement of gdp under product method
rishi ram khanal
 
Major social problem in nepal child labour socilology
Major social problem in nepal child labour socilology
rishi ram khanal
 
Light source ooad
Light source ooad
rishi ram khanal
 
Introduction to java
Introduction to java
rishi ram khanal
 
Introduction to artificial intelligence
Introduction to artificial intelligence
rishi ram khanal
 
Interview method in research
Interview method in research
rishi ram khanal
 
Presentation on kurtosis statistics
Presentation on kurtosis statistics
rishi ram khanal
 
Importance of double entry accounting system for public limited company (basi...
Importance of double entry accounting system for public limited company (basi...
rishi ram khanal
 
Implementation issues software engineering
Implementation issues software engineering
rishi ram khanal
 
Effect of migration in developing country
Effect of migration in developing country
rishi ram khanal
 
Goals of firm business finance
Goals of firm business finance
rishi ram khanal
 
General register organization (computer organization)
General register organization (computer organization)
rishi ram khanal
 
GDP and trends economics .rishi
GDP and trends economics .rishi
rishi ram khanal
 
Final internship-report on the networking department of the internet service ...
Final internship-report on the networking department of the internet service ...
rishi ram khanal
 
Field study of crystal finance share broker
Field study of crystal finance share broker
rishi ram khanal
 
Dijkstra algorithm
Dijkstra algorithm
rishi ram khanal
 
Computer virus and worms
Computer virus and worms
rishi ram khanal
 
Database management system
Database management system
rishi ram khanal
 
Credential reuse cyber security
Credential reuse cyber security
rishi ram khanal
 
Cisco packet tracer router
Cisco packet tracer router
rishi ram khanal
 
Measurement of gdp under product method
Measurement of gdp under product method
rishi ram khanal
 
Major social problem in nepal child labour socilology
Major social problem in nepal child labour socilology
rishi ram khanal
 
Introduction to artificial intelligence
Introduction to artificial intelligence
rishi ram khanal
 
Interview method in research
Interview method in research
rishi ram khanal
 
Presentation on kurtosis statistics
Presentation on kurtosis statistics
rishi ram khanal
 
Importance of double entry accounting system for public limited company (basi...
Importance of double entry accounting system for public limited company (basi...
rishi ram khanal
 
Implementation issues software engineering
Implementation issues software engineering
rishi ram khanal
 
Effect of migration in developing country
Effect of migration in developing country
rishi ram khanal
 
Goals of firm business finance
Goals of firm business finance
rishi ram khanal
 
General register organization (computer organization)
General register organization (computer organization)
rishi ram khanal
 
GDP and trends economics .rishi
GDP and trends economics .rishi
rishi ram khanal
 
Final internship-report on the networking department of the internet service ...
Final internship-report on the networking department of the internet service ...
rishi ram khanal
 
Field study of crystal finance share broker
Field study of crystal finance share broker
rishi ram khanal
 
Database management system
Database management system
rishi ram khanal
 
Credential reuse cyber security
Credential reuse cyber security
rishi ram khanal
 
Cisco packet tracer router
Cisco packet tracer router
rishi ram khanal
 
Ad

Recently uploaded (20)

MATERIAL SCIENCE LECTURE NOTES FOR DIPLOMA STUDENTS
MATERIAL SCIENCE LECTURE NOTES FOR DIPLOMA STUDENTS
SAMEER VISHWAKARMA
 
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Mark Billinghurst
 
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Mark Billinghurst
 
Complete University of Calculus :: 2nd edition
Complete University of Calculus :: 2nd edition
Shabista Imam
 
DESIGN OF REINFORCED CONCRETE ELEMENTS S
DESIGN OF REINFORCED CONCRETE ELEMENTS S
prabhusp8
 
Structural Wonderers_new and ancient.pptx
Structural Wonderers_new and ancient.pptx
nikopapa113
 
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
 
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
resming1
 
special_edition_using_visual_foxpro_6.pdf
special_edition_using_visual_foxpro_6.pdf
Shabista Imam
 
20CE404-Soil Mechanics - Slide Share PPT
20CE404-Soil Mechanics - Slide Share PPT
saravananr808639
 
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Mark Billinghurst
 
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL...
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL...
resming1
 
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
 
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Shabista Imam
 
Industry 4.o the fourth revolutionWeek-2.pptx
Industry 4.o the fourth revolutionWeek-2.pptx
KNaveenKumarECE
 
Cadastral Maps
Cadastral Maps
Google
 
Validating a Citizen Observatories enabling Platform by completing a Citizen ...
Validating a Citizen Observatories enabling Platform by completing a Citizen ...
Diego López-de-Ipiña González-de-Artaza
 
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Shabista Imam
 
Unit III_One Dimensional Consolidation theory
Unit III_One Dimensional Consolidation theory
saravananr808639
 
machine learning is a advance technology
machine learning is a advance technology
ynancy893
 
MATERIAL SCIENCE LECTURE NOTES FOR DIPLOMA STUDENTS
MATERIAL SCIENCE LECTURE NOTES FOR DIPLOMA STUDENTS
SAMEER VISHWAKARMA
 
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Rapid Prototyping for XR: Lecture 3 - Video and Paper Prototyping
Mark Billinghurst
 
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Mark Billinghurst
 
Complete University of Calculus :: 2nd edition
Complete University of Calculus :: 2nd edition
Shabista Imam
 
DESIGN OF REINFORCED CONCRETE ELEMENTS S
DESIGN OF REINFORCED CONCRETE ELEMENTS S
prabhusp8
 
Structural Wonderers_new and ancient.pptx
Structural Wonderers_new and ancient.pptx
nikopapa113
 
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
 
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
resming1
 
special_edition_using_visual_foxpro_6.pdf
special_edition_using_visual_foxpro_6.pdf
Shabista Imam
 
20CE404-Soil Mechanics - Slide Share PPT
20CE404-Soil Mechanics - Slide Share PPT
saravananr808639
 
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Mark Billinghurst
 
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL...
Data Structures Module 3 Binary Trees Binary Search Trees Tree Traversals AVL...
resming1
 
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
 
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Shabista Imam
 
Industry 4.o the fourth revolutionWeek-2.pptx
Industry 4.o the fourth revolutionWeek-2.pptx
KNaveenKumarECE
 
Cadastral Maps
Cadastral Maps
Google
 
Validating a Citizen Observatories enabling Platform by completing a Citizen ...
Validating a Citizen Observatories enabling Platform by completing a Citizen ...
Diego López-de-Ipiña González-de-Artaza
 
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Shabista Imam
 
Unit III_One Dimensional Consolidation theory
Unit III_One Dimensional Consolidation theory
saravananr808639
 
machine learning is a advance technology
machine learning is a advance technology
ynancy893
 

GUI (graphical user interface)

  • 2. Introduction to GUI • Graphical user interface is a type of user interface that allows users to interact with the screen using graphical components(Visual Indicators) rather than text commands. • There are two sets of Java APIs(Application Programming Interface) for graphics Programming: 1.AWT(Abstract Windowing Toolkit) 2.Swing
  • 3. AWT • It consist of 12 packages • Only 2 packages-java.awt and java.awt.event are commonly used • The java.awt package contains the core AWT graphics classes • GUI component classes(such as Button, Textfield ,label) • GUI container classes(frame ,panel,Dialog) • Layout mananger(flowlayout,Borderlayout,Gridlayout) • Custom graphic classes(Graphic,Color,Font)
  • 4. AWT Events • The java .awt.event package supports event handling • Event classes (ActionEvent, MouseEvent,KeyEvent and WindowEvent) • Event Listener Interfaces(Such as ActionListener,Mouselistener, WindowsListener)
  • 5. Container • A frame is the top –level container of an AWT GUI program • A panel is a rectangular area(or portion) used to group related GUI components • In a GUI program , a component must be kept in a container • Every container has a method called add(Component c)
  • 7. APPLET • An applet is a java program that runs in a Web browser • Applet is a container class like Frame • An applet is a java class that extends the java.applet.Applet class • Applets are designed to be embedded within an HTML page • When a user views an Html page that contains an applet ,the code for the applet is downloaded to the user’s machine • A JVM is required to view an applet
  • 8. PRATICAL • How to make an applet Program? • How a programmer can compile and run an applet code? • How applet can be embedded in the HTML code?
  • 9. LIFE CYCLE OF APPLET • JVM on the user’s machine creates an instance of the applet class and invokes various methods during the applet’s life time • Four method in the Applet class give you the framework on which you can build any applet Application • Init() • Start() • Stop() • Destroy() • Paint()(important function but not a part of Lifecycle)
  • 10. How to set Components in Applet • Component • Button • Textfield • Label • CheckBox • List • We need to design GUI so that user can interact with the applet • We have to place component on the applet container , to meet this requirement
  • 11. For pratical • Basic Skeleton of an applet • Creating Component reference variables • Initialize Components • Adding Components to the applet • Setting layout
  • 12. Event Handling in an Applet In java • Event Changing the state of an object is knows as Event. For example click on button, dragging mouse, minimizing window etc The java.awt.event package provides many event classes and Listener interfaces for event handling Event handling is to make java code ready to respond any particular event
  • 13. Feeling of a button Hey , I am button class object. user can click me, but I don’t know what to run when he does that
  • 14. Feeling of the programmer(you) I am the programmer , I have to write code that should be executed on click of a button Public void someFunction() { write some code } Struggling phase: But do my function get to know that button has just clicked?
  • 15. Two Ends Of Story Hey programmer , I have method to register to register your code with me Ok that means button.addActionListener();
  • 16. Communication can solve the issue Yes , but you have to pass an object to function Button.addActionListener(here?); But object of which class?
  • 17. See prototype defined in Button class Public void addActionListener(ActionListener, actionlistener) But ActionListener ia an interface, which I have to pass an object of class which implements ActionLIstener. Button.addActionListener(object);
  • 18. Ok, then I have to make a class which implements ActionListener Class Myhandler implements ActionListener { Public void someFunction (ActionEvent e) { write some code } }
  • 19. Button.addActionListener(new MyHandler()); This is the way I can register my handler with my Button object
  • 20. Now I have an object of Myhandler referenced by actionListener(a reference variable of ActionListener)
  • 21. Parameter Passing in an Applet <param> tag • Parameter are passed to applets in Name=Value pairs in <param> tags betweeen the opening and closing applet tags • Inside the applet , you read the values passed through the param tags with the getParameter() method of the java.applet.Applet class
  • 22. Import java.applet.Applet; Import java.awt.*; /*<applet code=“Myapplet” width=“300” height=“500” <param name=“saroj” value=“paras”></param></applet> Public class Myapplet extends Applet { String message=“hello”; Public void paint(Graphics s) { String s1=this.getParameter(“saroj”); if(s1==null) s1=message; s.drawString(s1,150,150); } }
  • 23. Introduction to AWT • Java Awt(Abstract Windowing Toolkit) is an API to develop GUI or window-based application in java. • AWT classes • GUI component classes(such as Button, Textfield ,label) • GUI container classes(frame ,panel,DialogBoxes) • Layout mananger(flowlayout,Borderlayout,Gridlayout) • Custom graphic classes(Graphic,Color,Font) • Event Handling classes(AWTEvent,AwteEventMultiCaster)
  • 24. Hierarchy of java AWT classes
  • 25. Frame • The Frame is the container that contain title bar and can have menu bars. It can have other components like button, textfield etc. • The window is the container that have no borders and menu bars. You must use frame, dialog or another window for creating a window. • The Panel is the container that doesn't contain title bar and menu bars. It can have other components like button, textfield etc.
  • 26. Working with Frame Window • Setting the window’s Dimensions Void setSize(init newWidth, init newHeight) void setSize(Dimension newSize) getSize() method is used to obtain current size of a window. • Hiding and showing a window void setVisible(boolean visibleFlag) Setting a window’s Title void setTitle(String newTitle)
  • 27. Example of AWT frame Import java.awt.*; Class MyFrame extends Frame { MyFrame() { setTitle(“Myfirst Frame”); setSize(200,200); Button b=new Button(“click Me”); setLayout(null); b.setBounds(50,50,50,50); add(b); setVisible(true); } }