SlideShare a Scribd company logo
1 of 13
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

Awt, Swing, Layout managers
Awt, Swing, Layout managersAwt, Swing, Layout managers
Awt, Swing, Layout managersswapnac12
 
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 programmingSynapseindiappsdevelopment
 
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 functionsJack Lee
 
The AWT and Swing
The AWT and SwingThe AWT and Swing
The AWT and Swingadil 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, .pdfoptokunal1
 
Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892Swingpre 150616004959-lva1-app6892
Swingpre 150616004959-lva1-app6892renuka gavli
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in javaAdil Mehmoood
 
01. introduction to swing
01. introduction to swing01. introduction to swing
01. introduction to swingPrashant 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.pdfbirajdar2
 
Unit-2 swing and mvc architecture
Unit-2 swing and mvc architectureUnit-2 swing and mvc architecture
Unit-2 swing and mvc architectureAmol 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

Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 

Recently uploaded (20)

Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 

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.