SlideShare a Scribd company logo
A Simple Java GUI Application:
In this tutorial we will design a JFrame
window which contains a JButton.
When the user clicks on the JButton
a message dialog appears.
A Simple Java GUI Application:
Steps:
1. Start the Eclipse IDE.
2. Create a Java Project with any name, for example
GUIJavaProject.
3. Inside the GUIJavaProject, create a class with any name,
for example GUIJavaClass, with a main method inside
the class.
A Simple Java GUI Application:
4. Make the GUIJavaClass class extends the JFrame class.
5. Make the GUIJavaClass class implements the
ActionListener interface.
Additional import statements are required to define:
• JFrame class
• ActionListener interface.
public class GUIJavaClass extends JFrame implements ActionListener
A Simple Java GUI Application:
6. Inside the GUIJavaClass class add the followings:
• Definition for the JButton object (B1).
• Definition for an empty constructor method.
• Definition for the method actionPerformed method.
Additional import statements are required to define:
• JButton class
• ActionEvent class
public class GUIJavaClass extends JFrame implements ActionListener
{
private JButton B1 ;
public MyClass( )
{
}
public void actionPerformed ( ActionEvent e )
{
}
public static void main ( String [ ] args )
{
}
}
A Simple Java GUI Application:
7. Inside the empty constructor method add the following
code.
Additional import statement is required to define the
FlowLayout class.
public GUIJavaClass( )
{
//setLayout: sets the size and position of the JButton within the JFrame.
setLayout(new FlowLayout());
//Create the JButton (B1) with the statement "Click" written inside JButton.
B1 = new JButton ("Click");
/*
Add action listener to (B1) to listen to event from it.
event is triggered here if user click on (B1).
*/
B1.addActionListener(this);
//Add (B1) to the JFrame.
add (B1);
}
A Simple Java GUI Application:
8. Inside the actionPerformed method add the following
code.
Additional import statement is required to define the
JOptionPane class.
public void actionPerformed( ActionEvent e )
{
/*
If user clicks on (B1) the message dialog with the message
"Button was clicked" appears.
*/
if ( e.getSource() == B1 )
JOptionPane.showMessageDialog( this, "Button was clicked");
}
A Simple Java GUI Application:
9. Inside the main method add the following code.
public static void main( String [ ] args )
{
/* Define and create the GUIJavaClass object (frame),
this will call the empty constructor. */
GUIJavaClass frame = new GUIJavaClass();
//set the title for the JFrame window to "GUI Example".
frame.setTitle("GUI Example");
/* set the width for the JFrame window width to 400,
and height to 500. */
frame.setSize(400, 500);
/* set the location for the JFrame window to 400 on x-axis,
and to 100 to y-axis. */
frame.setLocation(400,100);
/* make the JFrame window close when the user click
on the X button inside the JFrame's title bar. */
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// make the JFrame window appear to user.
frame.setVisible(true);
}
A Simple Java GUI Application:
10. Execute the code.
Inside the JFrame window, when the button is clicked, a
message dialog will appear with the statement “Button
was clicked” inside.

More Related Content

Similar to A Simple Java GUI Application.pptx

Tuto jtatoo
Tuto jtatooTuto jtatoo
Awt, Swing, Layout managers
Awt, Swing, Layout managersAwt, Swing, Layout managers
Awt, Swing, Layout managers
swapnac12
 
Lecture9 oopj
Lecture9 oopjLecture9 oopj
Lecture9 oopj
Dhairya Joshi
 
GUI Programming with Java
GUI Programming with JavaGUI Programming with Java
GUI Programming with Java
Jussi Pohjolainen
 
Synapseindia dotnet development chapter 14 event-driven programming
Synapseindia dotnet development  chapter 14 event-driven programmingSynapseindia dotnet development  chapter 14 event-driven programming
Synapseindia dotnet development chapter 14 event-driven programming
Synapseindiappsdevelopment
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
Tareq Hasan
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
shanmuga rajan
 
Z blue introduction to gui (39023299)
Z blue   introduction to gui (39023299)Z blue   introduction to gui (39023299)
Z blue introduction to gui (39023299)
Narayana Swamy
 
CrossUI Tutorial - Basic - Calling JS functions
CrossUI Tutorial  - Basic - Calling JS functionsCrossUI Tutorial  - Basic - Calling JS functions
CrossUI Tutorial - Basic - Calling JS functions
Jack Lee
 
The AWT and Swing
The AWT and SwingThe AWT and Swing
The AWT and Swing
adil raja
 
1) Write a shortsnippetofcodethatcreates a J Panel objectcalled p1, .pdf
1) Write a shortsnippetofcodethatcreates a J Panel objectcalled p1, .pdf1) Write a shortsnippetofcodethatcreates a J Panel objectcalled p1, .pdf
1) Write a shortsnippetofcodethatcreates a J Panel objectcalled p1, .pdf
optokunal1
 
SwingApplet.pptx
SwingApplet.pptxSwingApplet.pptx
SwingApplet.pptx
GEETHAS668001
 
Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892
renuka gavli
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in java
Adil Mehmoood
 
Swings
SwingsSwings
UIAutomator
UIAutomatorUIAutomator
UIAutomator
Sandip Ganguli
 
Md11 gui event handling
Md11 gui event handlingMd11 gui event handling
Md11 gui event handling
Rakesh Madugula
 
01. introduction to swing
01. introduction to swing01. introduction to swing
01. introduction to swing
Prashant Mehta
 
Throughout the semester, we have been working on command line applic.pdf
Throughout the semester, we have been working on command line applic.pdfThroughout the semester, we have been working on command line applic.pdf
Throughout the semester, we have been working on command line applic.pdf
birajdar2
 
Unit-2 swing and mvc architecture
Unit-2 swing and mvc architectureUnit-2 swing and mvc architecture
Unit-2 swing and mvc architecture
Amol Gaikwad
 

Similar to A Simple Java GUI Application.pptx (20)

Tuto jtatoo
Tuto jtatooTuto jtatoo
Tuto jtatoo
 
Awt, Swing, Layout managers
Awt, Swing, Layout managersAwt, Swing, Layout managers
Awt, Swing, Layout managers
 
Lecture9 oopj
Lecture9 oopjLecture9 oopj
Lecture9 oopj
 
GUI Programming with Java
GUI Programming with JavaGUI Programming with Java
GUI Programming with Java
 
Synapseindia dotnet development chapter 14 event-driven programming
Synapseindia dotnet development  chapter 14 event-driven programmingSynapseindia dotnet development  chapter 14 event-driven programming
Synapseindia dotnet development chapter 14 event-driven programming
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
Z blue introduction to gui (39023299)
Z blue   introduction to gui (39023299)Z blue   introduction to gui (39023299)
Z blue introduction to gui (39023299)
 
CrossUI Tutorial - Basic - Calling JS functions
CrossUI Tutorial  - Basic - Calling JS functionsCrossUI Tutorial  - Basic - Calling JS functions
CrossUI Tutorial - Basic - Calling JS functions
 
The AWT and Swing
The AWT and SwingThe AWT and Swing
The AWT and Swing
 
1) Write a shortsnippetofcodethatcreates a J Panel objectcalled p1, .pdf
1) Write a shortsnippetofcodethatcreates a J Panel objectcalled p1, .pdf1) Write a shortsnippetofcodethatcreates a J Panel objectcalled p1, .pdf
1) Write a shortsnippetofcodethatcreates a J Panel objectcalled p1, .pdf
 
SwingApplet.pptx
SwingApplet.pptxSwingApplet.pptx
SwingApplet.pptx
 
Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in java
 
Swings
SwingsSwings
Swings
 
UIAutomator
UIAutomatorUIAutomator
UIAutomator
 
Md11 gui event handling
Md11 gui event handlingMd11 gui event handling
Md11 gui event handling
 
01. introduction to swing
01. introduction to swing01. introduction to swing
01. introduction to swing
 
Throughout the semester, we have been working on command line applic.pdf
Throughout the semester, we have been working on command line applic.pdfThroughout the semester, we have been working on command line applic.pdf
Throughout the semester, we have been working on command line applic.pdf
 
Unit-2 swing and mvc architecture
Unit-2 swing and mvc architectureUnit-2 swing and mvc architecture
Unit-2 swing and mvc architecture
 

Recently uploaded

The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
kalichargn70th171
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
Sven Peters
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
Green Software Development
 
SQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure MalaysiaSQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure Malaysia
GohKiangHock
 
Project Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdfProject Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdf
Karya Keeper
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
Peter Muessig
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 
zOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL DifferenceszOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL Differences
YousufSait3
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
Liberarsi dai framework con i Web Component.pptx
Liberarsi dai framework con i Web Component.pptxLiberarsi dai framework con i Web Component.pptx
Liberarsi dai framework con i Web Component.pptx
Massimo Artizzu
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
ShulagnaSarkar2
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
mz5nrf0n
 
What next after learning python programming basics
What next after learning python programming basicsWhat next after learning python programming basics
What next after learning python programming basics
Rakesh Kumar R
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
safelyiotech
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
Octavian Nadolu
 
Preparing Non - Technical Founders for Engaging a Tech Agency
Preparing Non - Technical Founders for Engaging  a  Tech AgencyPreparing Non - Technical Founders for Engaging  a  Tech Agency
Preparing Non - Technical Founders for Engaging a Tech Agency
ISH Technologies
 
Lecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptxLecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptx
TaghreedAltamimi
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
kalichargn70th171
 

Recently uploaded (20)

The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
 
Microservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we workMicroservice Teams - How the cloud changes the way we work
Microservice Teams - How the cloud changes the way we work
 
Energy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina JonuziEnergy consumption of Database Management - Florina Jonuzi
Energy consumption of Database Management - Florina Jonuzi
 
SQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure MalaysiaSQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure Malaysia
 
Project Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdfProject Management: The Role of Project Dashboards.pdf
Project Management: The Role of Project Dashboards.pdf
 
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 
zOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL DifferenceszOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL Differences
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
Liberarsi dai framework con i Web Component.pptx
Liberarsi dai framework con i Web Component.pptxLiberarsi dai framework con i Web Component.pptx
Liberarsi dai framework con i Web Component.pptx
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
 
What next after learning python programming basics
What next after learning python programming basicsWhat next after learning python programming basics
What next after learning python programming basics
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
 
Preparing Non - Technical Founders for Engaging a Tech Agency
Preparing Non - Technical Founders for Engaging  a  Tech AgencyPreparing Non - Technical Founders for Engaging  a  Tech Agency
Preparing Non - Technical Founders for Engaging a Tech Agency
 
Lecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptxLecture 2 - software testing SE 412.pptx
Lecture 2 - software testing SE 412.pptx
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
 

A Simple Java GUI Application.pptx

  • 1. A Simple Java GUI Application: In this tutorial we will design a JFrame window which contains a JButton. When the user clicks on the JButton a message dialog appears.
  • 2. A Simple Java GUI Application: Steps: 1. Start the Eclipse IDE. 2. Create a Java Project with any name, for example GUIJavaProject. 3. Inside the GUIJavaProject, create a class with any name, for example GUIJavaClass, with a main method inside the class.
  • 3. A Simple Java GUI Application: 4. Make the GUIJavaClass class extends the JFrame class. 5. Make the GUIJavaClass class implements the ActionListener interface. Additional import statements are required to define: • JFrame class • ActionListener interface.
  • 4. public class GUIJavaClass extends JFrame implements ActionListener
  • 5. A Simple Java GUI Application: 6. Inside the GUIJavaClass class add the followings: • Definition for the JButton object (B1). • Definition for an empty constructor method. • Definition for the method actionPerformed method. Additional import statements are required to define: • JButton class • ActionEvent class
  • 6. public class GUIJavaClass extends JFrame implements ActionListener { private JButton B1 ; public MyClass( ) { } public void actionPerformed ( ActionEvent e ) { } public static void main ( String [ ] args ) { } }
  • 7. A Simple Java GUI Application: 7. Inside the empty constructor method add the following code. Additional import statement is required to define the FlowLayout class.
  • 8. public GUIJavaClass( ) { //setLayout: sets the size and position of the JButton within the JFrame. setLayout(new FlowLayout()); //Create the JButton (B1) with the statement "Click" written inside JButton. B1 = new JButton ("Click"); /* Add action listener to (B1) to listen to event from it. event is triggered here if user click on (B1). */ B1.addActionListener(this); //Add (B1) to the JFrame. add (B1); }
  • 9. A Simple Java GUI Application: 8. Inside the actionPerformed method add the following code. Additional import statement is required to define the JOptionPane class.
  • 10. public void actionPerformed( ActionEvent e ) { /* If user clicks on (B1) the message dialog with the message "Button was clicked" appears. */ if ( e.getSource() == B1 ) JOptionPane.showMessageDialog( this, "Button was clicked"); }
  • 11. A Simple Java GUI Application: 9. Inside the main method add the following code.
  • 12. public static void main( String [ ] args ) { /* Define and create the GUIJavaClass object (frame), this will call the empty constructor. */ GUIJavaClass frame = new GUIJavaClass(); //set the title for the JFrame window to "GUI Example". frame.setTitle("GUI Example"); /* set the width for the JFrame window width to 400, and height to 500. */ frame.setSize(400, 500); /* set the location for the JFrame window to 400 on x-axis, and to 100 to y-axis. */ frame.setLocation(400,100); /* make the JFrame window close when the user click on the X button inside the JFrame's title bar. */ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // make the JFrame window appear to user. frame.setVisible(true); }
  • 13. A Simple Java GUI Application: 10. Execute the code. Inside the JFrame window, when the button is clicked, a message dialog will appear with the statement “Button was clicked” inside.