SlideShare a Scribd company logo
1 of 27
Download to read offline
GUI Programming with Java
Java Provides 2 Frameworks for building GUI-based applications.
Those are
• AWT-(Abstract Window Toolkit)
• Swing AWT-(Abstract Window Toolkit)
• AWT (Abstract Window Toolkit) is an API to develop GUI or window-
based applications in java.
• The java.awt package provides classes for AWT API such as TextField,
Label, TextArea, Checkbox, Choice, List etc.
• AWT components are platform-dependent i.e. components are displayed
according to the view of operating system.
• Component: Component is an abstract class that contains various classes
such as Button, Label,Checkbox,TextField, Menu and etc.
• Container: The Container is a component in AWT that can contain
another components like buttons, textfields, labels etc. The Container class
extends Frame and Panel.
• Window: The window is the container that have no borders and menu
bars. You must use frame for creating a window.
• Frame: The Frame is the container that contain title bar and can have
menu bars. It can have other components like button, textfield etc.
• Panel: The Panel is the container that doesn't contain title bar and menu
bars. It can have other components like button, textfield etc.
To create simple awt example, you need a frame.
There are two ways to create a frame in AWT.
• By extending Frame class (inheritance)
Ex: class Example extends Frame
{
……..
}
• By creating the object of Frame class (association)
Ex: class Example
{
Frame obj=new Frame();
……..
}
import java.awt.*;
public class AwtExample { public static void main(String[] args)
{
Frame f=new Frame();
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
f.setTitle("Simple Example");
}
}
Output: Javac AwtExample.java
Java AwtExample
To create simple awt example, you need a frame.
There are two ways to create a frame in AWT.
• By extending Frame class (inheritance)
Ex: class Example extends Frame { …….. }
• By creating the object of Frame class (association)
Ex: class Example { Frame obj=new Frame(); …….. }
Swing
Swing is a framework or API that is used to create GUI (or) window-based
applications.It is an advanced version of AWT (Abstract Window Toolkit)
API and entirely written in java.
Unlike AWT, Java Swing provides platform-independent and lightweight
components.
The javax.swing package provides classes for java swing API such as
JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu,
JColorChooser etc.
To create simple swing example, you need a frame.
In swing, we use JFrame class to create a frame. There are two ways to
create a frame in swing.
• By extending JFrame class (inheritance)
Ex: class Example extends JFrame
{
……..
}
• By creating the object of JFrame class (association)
Ex: class Example
{
JFrame obj=new JFrame();
……..
}
A Simple Swing Example We can write the code of swing inside the main() or
constructor. In Main() Method:
SwingExample.java
import javax.swing.*;
public class SwingExample
{
public static void main(String[] args)
{
JFrame f=new JFrame("Simple Swing Example");
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
}
Applet
An applet is a Java program that runs in a Web browser.
(or)
Applet is a special type of program that is embedded in the webpage to generate the
dynamic content.
It runs inside the browser and works at client side.
Any applet in Java is a class that extends the java.applet.Applet class.
Advantage of Applet
There are many advantages of applet.
They are as follows: • It works at client side so less response time.
• Secured
• It can be executed by browsers running under many platforms, including Linux,
Windows, Mac Os etc.
As displayed in the diagram, Applet class extends Panel.
Panel class extends Container, which is the subclass of Component.
Where Object class is base class for all the classes in java.
JApplet class is extension of Applet class.
Lifecycle of Applet: There are 5 lifecycle methods of Applet, Those are public
void init(): is used to initialized the Applet. It is invoked only once.
public void start(): is invoked after the init() method or browser is maximized.
It is used to start the Applet.
public void paint(Graphics g): is invoked immediately after the start()
method, and this method helps to create Applet’s GUI such as a colored
background, drawing and writing.
public void stop(): is used to stop the Applet. It is invoked when Applet is stop
or browser is minimized.
public void destroy(): is used to destroy the Applet. It is invoked only once.
Remember:
java.applet.Applet class provides 4 methods (init,start,stop & destroy) and
java.awt.Graphics class provides 1 method ( paint) to createApplet.
Simple example of Applet:
To execute an Applet, First Create an applet and compile it just like a simple java
program. First.java
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet
{
public void paint(Graphics g)
{
g.drawString(“Welcome to Applet",50,150);
}
}
Compile: D:> javac First.java
After successful compilation, we get First.class file.
After that create an html file and place the applet code in html file.
Displaying Graphics in Applet:
java.awt.Graphics class provides many methods for graphics programming.
The Commonly used methods of Graphics class:
• drawString(String str, int x, int y): is used to draw the specified string.
• drawRect(int x, int y, int width, int height): draws a rectangle with the specified width and
height.
• fillRect(int x, int y, int width, int height): is used to fill rectangle with the default color and
specified width and height.
• drawOval(int x, int y, int width, int height): is used to draw oval with the specified width
and height.
• fillOval(int x, int y, int width, int height): is used to fill oval with the default color and
specified width and height.
• drawLine(int x1, int y1, int x2, int y2): is used to draw line between the points(x1, y1) and
(x2, y2).
• setColor(Color c): is used to set the graphics current color to the specified color.
• setFont(Font font): is used to set the graphics current font to the specified font.
Example: GraphicsDemo.java import java.applet.Applet; import java.awt.*;
public class GraphicsDemo extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawString("Welcome",50, 50);
g.drawLine(20,30,20,300);
g.drawRect(70,100,30,30);
g.fillRect(170,100,30,30);
g.drawOval(70,200,30,30);
g.setColor(Color.pink);
g.fillOval(170,200,30,30);
}
}
Components of Applet:
The components of AWT are the components of Applet,i.e we can use AWT components
(Button,TextField,Checkbox, TextArea,Choice & etc.…) in applet.
As we perform event handling in AWT or Swing, we can perform it in applet also.
Let's see the simple example of components and event handling in applet that prints a
message by click on the button.
Example: AppletComponents.
java import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class AppletComponents extends Applet implements ActionListener
{
Button b; TextField tf; public void init()
{
tf=new TextField();
tf.setBounds(80,40,150,20);
b=new Button("Click");
b.setBounds(80,120,80,30);
add(b);add(tf);
b.addActionListener(this);
setLayout(null);
}
public void actionPerformed(ActionEvent e)
{
tf.setText("Welcome"); } }
JApplet Class: As we prefer Swing to AWT.
Now we can use JApplet that can have all the controls of swing.
The JApplet class extends the Applet class. The components of swing are the
components of JApplet,i.e we can use swing components
(JButton,JTextField,JCheckBox, JTextArea,JList & etc.…) in JApplet.
GUI.pdf
GUI.pdf

More Related Content

Similar to GUI.pdf (20)

Basic of Applet
Basic of AppletBasic of Applet
Basic of Applet
 
AdvancedJava.pptx
AdvancedJava.pptxAdvancedJava.pptx
AdvancedJava.pptx
 
Java Applet
Java AppletJava Applet
Java Applet
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Java
 
13457272.ppt
13457272.ppt13457272.ppt
13457272.ppt
 
Applets
AppletsApplets
Applets
 
Applets - lev' 2
Applets - lev' 2Applets - lev' 2
Applets - lev' 2
 
08graphics
08graphics08graphics
08graphics
 
Unit 7 Java
Unit 7 JavaUnit 7 Java
Unit 7 Java
 
JAVA PPT -5 BY ADI.pdf
JAVA PPT -5 BY ADI.pdfJAVA PPT -5 BY ADI.pdf
JAVA PPT -5 BY ADI.pdf
 
AWT.pptx
AWT.pptxAWT.pptx
AWT.pptx
 
first-applet
first-appletfirst-applet
first-applet
 
Chap1 1 1
Chap1 1 1Chap1 1 1
Chap1 1 1
 
Chap1 1.1
Chap1 1.1Chap1 1.1
Chap1 1.1
 
SwingApplet.pptx
SwingApplet.pptxSwingApplet.pptx
SwingApplet.pptx
 
27 applet programming
27  applet programming27  applet programming
27 applet programming
 
Ajp notes-chapter-01
Ajp notes-chapter-01Ajp notes-chapter-01
Ajp notes-chapter-01
 
Applet and graphics programming
Applet and graphics programmingApplet and graphics programming
Applet and graphics programming
 
object oriented programming examples
object oriented programming examplesobject oriented programming examples
object oriented programming examples
 
Ajp notes-chapter-01
Ajp notes-chapter-01Ajp notes-chapter-01
Ajp notes-chapter-01
 

More from Poornima E.G.

Android Application managing activites.pptx
Android Application managing activites.pptxAndroid Application managing activites.pptx
Android Application managing activites.pptxPoornima E.G.
 
ROUND4GuestFacultyAllotmentList,Noworkloadcandidates.pdf
ROUND4GuestFacultyAllotmentList,Noworkloadcandidates.pdfROUND4GuestFacultyAllotmentList,Noworkloadcandidates.pdf
ROUND4GuestFacultyAllotmentList,Noworkloadcandidates.pdfPoornima E.G.
 
VTU MCA 2022 JAVA Exceeption Handling.docx
VTU MCA  2022 JAVA Exceeption Handling.docxVTU MCA  2022 JAVA Exceeption Handling.docx
VTU MCA 2022 JAVA Exceeption Handling.docxPoornima E.G.
 
Operating System basics Introduction
Operating System basics IntroductionOperating System basics Introduction
Operating System basics IntroductionPoornima E.G.
 
csharp_dotnet_adnanreza.pptx
csharp_dotnet_adnanreza.pptxcsharp_dotnet_adnanreza.pptx
csharp_dotnet_adnanreza.pptxPoornima E.G.
 
Millimeter wave mobile communications for 5 g Cellular
Millimeter wave mobile communications for 5 g CellularMillimeter wave mobile communications for 5 g Cellular
Millimeter wave mobile communications for 5 g CellularPoornima E.G.
 
Semantic open io t service platform technology
Semantic open io t service platform technologySemantic open io t service platform technology
Semantic open io t service platform technologyPoornima E.G.
 

More from Poornima E.G. (8)

Android Application managing activites.pptx
Android Application managing activites.pptxAndroid Application managing activites.pptx
Android Application managing activites.pptx
 
ROUND4GuestFacultyAllotmentList,Noworkloadcandidates.pdf
ROUND4GuestFacultyAllotmentList,Noworkloadcandidates.pdfROUND4GuestFacultyAllotmentList,Noworkloadcandidates.pdf
ROUND4GuestFacultyAllotmentList,Noworkloadcandidates.pdf
 
VTU MCA 2022 JAVA Exceeption Handling.docx
VTU MCA  2022 JAVA Exceeption Handling.docxVTU MCA  2022 JAVA Exceeption Handling.docx
VTU MCA 2022 JAVA Exceeption Handling.docx
 
Process and Thread
Process and Thread Process and Thread
Process and Thread
 
Operating System basics Introduction
Operating System basics IntroductionOperating System basics Introduction
Operating System basics Introduction
 
csharp_dotnet_adnanreza.pptx
csharp_dotnet_adnanreza.pptxcsharp_dotnet_adnanreza.pptx
csharp_dotnet_adnanreza.pptx
 
Millimeter wave mobile communications for 5 g Cellular
Millimeter wave mobile communications for 5 g CellularMillimeter wave mobile communications for 5 g Cellular
Millimeter wave mobile communications for 5 g Cellular
 
Semantic open io t service platform technology
Semantic open io t service platform technologySemantic open io t service platform technology
Semantic open io t service platform technology
 

Recently uploaded

_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 

Recently uploaded (20)

_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 

GUI.pdf

  • 1. GUI Programming with Java Java Provides 2 Frameworks for building GUI-based applications. Those are • AWT-(Abstract Window Toolkit) • Swing AWT-(Abstract Window Toolkit) • AWT (Abstract Window Toolkit) is an API to develop GUI or window- based applications in java. • The java.awt package provides classes for AWT API such as TextField, Label, TextArea, Checkbox, Choice, List etc. • AWT components are platform-dependent i.e. components are displayed according to the view of operating system.
  • 2.
  • 3. • Component: Component is an abstract class that contains various classes such as Button, Label,Checkbox,TextField, Menu and etc. • Container: The Container is a component in AWT that can contain another components like buttons, textfields, labels etc. The Container class extends Frame and Panel. • Window: The window is the container that have no borders and menu bars. You must use frame for creating a window. • Frame: The Frame is the container that contain title bar and can have menu bars. It can have other components like button, textfield etc. • Panel: The Panel is the container that doesn't contain title bar and menu bars. It can have other components like button, textfield etc.
  • 4.
  • 5. To create simple awt example, you need a frame. There are two ways to create a frame in AWT. • By extending Frame class (inheritance) Ex: class Example extends Frame { …….. } • By creating the object of Frame class (association) Ex: class Example { Frame obj=new Frame(); …….. }
  • 6. import java.awt.*; public class AwtExample { public static void main(String[] args) { Frame f=new Frame(); f.setSize(400,400); f.setLayout(null); f.setVisible(true); f.setTitle("Simple Example"); } } Output: Javac AwtExample.java Java AwtExample
  • 7. To create simple awt example, you need a frame. There are two ways to create a frame in AWT. • By extending Frame class (inheritance) Ex: class Example extends Frame { …….. } • By creating the object of Frame class (association) Ex: class Example { Frame obj=new Frame(); …….. }
  • 8. Swing Swing is a framework or API that is used to create GUI (or) window-based applications.It is an advanced version of AWT (Abstract Window Toolkit) API and entirely written in java. Unlike AWT, Java Swing provides platform-independent and lightweight components. The javax.swing package provides classes for java swing API such as JButton, JTextField, JTextArea, JRadioButton, JCheckbox, JMenu, JColorChooser etc.
  • 9.
  • 10.
  • 11.
  • 12. To create simple swing example, you need a frame. In swing, we use JFrame class to create a frame. There are two ways to create a frame in swing. • By extending JFrame class (inheritance) Ex: class Example extends JFrame { …….. } • By creating the object of JFrame class (association) Ex: class Example { JFrame obj=new JFrame(); …….. }
  • 13. A Simple Swing Example We can write the code of swing inside the main() or constructor. In Main() Method: SwingExample.java import javax.swing.*; public class SwingExample { public static void main(String[] args) { JFrame f=new JFrame("Simple Swing Example"); f.setSize(300,300); f.setLayout(null); f.setVisible(true); } }
  • 14. Applet An applet is a Java program that runs in a Web browser. (or) Applet is a special type of program that is embedded in the webpage to generate the dynamic content. It runs inside the browser and works at client side. Any applet in Java is a class that extends the java.applet.Applet class. Advantage of Applet There are many advantages of applet. They are as follows: • It works at client side so less response time. • Secured • It can be executed by browsers running under many platforms, including Linux, Windows, Mac Os etc.
  • 15.
  • 16. As displayed in the diagram, Applet class extends Panel. Panel class extends Container, which is the subclass of Component. Where Object class is base class for all the classes in java. JApplet class is extension of Applet class. Lifecycle of Applet: There are 5 lifecycle methods of Applet, Those are public void init(): is used to initialized the Applet. It is invoked only once. public void start(): is invoked after the init() method or browser is maximized. It is used to start the Applet. public void paint(Graphics g): is invoked immediately after the start() method, and this method helps to create Applet’s GUI such as a colored background, drawing and writing. public void stop(): is used to stop the Applet. It is invoked when Applet is stop or browser is minimized. public void destroy(): is used to destroy the Applet. It is invoked only once.
  • 17.
  • 18. Remember: java.applet.Applet class provides 4 methods (init,start,stop & destroy) and java.awt.Graphics class provides 1 method ( paint) to createApplet.
  • 19. Simple example of Applet: To execute an Applet, First Create an applet and compile it just like a simple java program. First.java import java.applet.Applet; import java.awt.Graphics; public class First extends Applet { public void paint(Graphics g) { g.drawString(“Welcome to Applet",50,150); } } Compile: D:> javac First.java After successful compilation, we get First.class file. After that create an html file and place the applet code in html file.
  • 20. Displaying Graphics in Applet: java.awt.Graphics class provides many methods for graphics programming. The Commonly used methods of Graphics class: • drawString(String str, int x, int y): is used to draw the specified string. • drawRect(int x, int y, int width, int height): draws a rectangle with the specified width and height. • fillRect(int x, int y, int width, int height): is used to fill rectangle with the default color and specified width and height. • drawOval(int x, int y, int width, int height): is used to draw oval with the specified width and height. • fillOval(int x, int y, int width, int height): is used to fill oval with the default color and specified width and height. • drawLine(int x1, int y1, int x2, int y2): is used to draw line between the points(x1, y1) and (x2, y2). • setColor(Color c): is used to set the graphics current color to the specified color. • setFont(Font font): is used to set the graphics current font to the specified font.
  • 21. Example: GraphicsDemo.java import java.applet.Applet; import java.awt.*; public class GraphicsDemo extends Applet { public void paint(Graphics g) { g.setColor(Color.red); g.drawString("Welcome",50, 50); g.drawLine(20,30,20,300); g.drawRect(70,100,30,30); g.fillRect(170,100,30,30); g.drawOval(70,200,30,30); g.setColor(Color.pink); g.fillOval(170,200,30,30); } }
  • 22.
  • 23. Components of Applet: The components of AWT are the components of Applet,i.e we can use AWT components (Button,TextField,Checkbox, TextArea,Choice & etc.…) in applet. As we perform event handling in AWT or Swing, we can perform it in applet also. Let's see the simple example of components and event handling in applet that prints a message by click on the button. Example: AppletComponents. java import java.applet.*; import java.awt.*; import java.awt.event.*; public class AppletComponents extends Applet implements ActionListener { Button b; TextField tf; public void init() { tf=new TextField(); tf.setBounds(80,40,150,20); b=new Button("Click"); b.setBounds(80,120,80,30); add(b);add(tf); b.addActionListener(this); setLayout(null); } public void actionPerformed(ActionEvent e) { tf.setText("Welcome"); } }
  • 24.
  • 25. JApplet Class: As we prefer Swing to AWT. Now we can use JApplet that can have all the controls of swing. The JApplet class extends the Applet class. The components of swing are the components of JApplet,i.e we can use swing components (JButton,JTextField,JCheckBox, JTextArea,JList & etc.…) in JApplet.