SlideShare a Scribd company logo
SWING
Created by – Samyak Jain (MCA/25014/2022)
Abhishek Ahawlat (MCA/25017/2022)
submitted to – Santosh Kumar Sir
What is Java Swing?
 Java Swing is a part of Java Foundation Classes(JFC)
that is used to create window-based applications. It is
built on top of AWT(Abstract Window Toolkit) API and
entirely written in Java.
 The javax.swing package provides classes for java swing
API such as
Jbutton,JTextField,JTextArea,JRadioButton,Jcheckbox
,Jmenu,JColorChooser etc.
Difference Between AWT and Swing
Components and Containers
 The Components class is the superclass of all components.
 Swing components are derived from the JComponent class.
 A Containers is a special type of component that is designed to
hold other components.
 The most commonly used containers are JFrame, JPanel and
Jwindow etc.
Important Method of Component class
• Void add(component c) // used to add swing component to a container
• Void setSize(int width,int height) //use to set the size of the container
• Void setLayout(Layout Manager m) //use to set the Layout
• Void setVisible(Boolean b) //to make the frame visible on the window.
Hierarchical Structure of Components
JFrame
• An extension of java.awt.Frame class ,it is the Swing’s version of applet.
• JFrame has the option to hide or close the window with the help of
setDefaultCloseOperation(int) method.
• Example: output :
import javax.swing.*;
public class NewClass
{
public static void main(String args[])
{
JFrame jf = new JFrame("Swing Example");
jf.setSize(300,300);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Another way of doing it…
import javax.swing.*;
public class NewClass extends JFrame
{
public NewClass() {};
public NewClass(String s)
{
super(s);
}
public static void main(String args[])
{
NewClass jf = new NewClass("Swing example");
jf.setSize(300,300);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
JTextField
The object of a JTextField class is a text component that allows the editing of
a single line text. It inherits JTextComponent class.
JLabel
The object of JLabel class is a component for placing text in a container. It is
used to display a single line of read only text. The text can be changed by an
application but a user cannot edit it directly. It inherits JComponent class.
Swing Example
import javax.swing.*;
import java.awt.event.*;
public class NewClass extends JFrame
{
JLabel l1,l2,l3,l4;
JTextField t1, t2;
JButton b1;
public NewClass() {};
public NewClass(String s)
{
super(s);
}
public void setComponents()
{
l1 = new JLabel("Addition of two Numbers ");
l2 = new JLabel("First Number");
l3 = new JLabel("Second Number");
l4 = new JLabel();
t1 = new JTextField();
t2 = new JTextField();
b1 = new JButton("Add");
setLayout(null);
l1.setBounds(50,50,200,20);
l2.setBounds(50,80,100,20);
t1.setBounds(150,80,100,20);
l3.setBounds(50,130,100,20);
t2.setBounds(150,130,100,20);
b1.setBounds(80,180,100,20);
l4.setBounds(50,240,200,20);
b1.addActionListener(new Handler());
add(l1);
add(l2);
add(t1);
add(l3);
add(t2);
add(b1);
add(l4);
}
class Handler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int a = Integer.parseInt(t1.getText());
int b = Integer.parseInt(t2.getText());
int s = a+b;
l3.setText("Result is "+s);
}
}
public static void main(String args[])
{
NewClass jf = new NewClass("Swing example");
jf.setComponents();
jf.setSize(500,500);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
THANK YOU

More Related Content

Similar to SWING.pptx

SWING USING JAVA WITH VARIOUS COMPONENTS
SWING USING  JAVA WITH VARIOUS COMPONENTSSWING USING  JAVA WITH VARIOUS COMPONENTS
SWING USING JAVA WITH VARIOUS COMPONENTS
bharathiv53
 
Java swing and events
Java swing and eventsJava swing and events
Java swing and events
Prathamesh sawant
 
Logic and Coding of Java Interfaces & Swing Applications
Logic and Coding of Java Interfaces & Swing ApplicationsLogic and Coding of Java Interfaces & Swing Applications
Logic and Coding of Java Interfaces & Swing Applications
kjkleindorfer
 
02 basic java programming and operators
02 basic java programming and operators02 basic java programming and operators
02 basic java programming and operators
Danairat Thanabodithammachari
 
jframe, jtextarea and jscrollpane
  jframe, jtextarea and jscrollpane  jframe, jtextarea and jscrollpane
jframe, jtextarea and jscrollpane
Mr. Akaash
 
شرح مقرر البرمجة 2 لغة جافا - الوحدة التاسعة
شرح مقرر البرمجة 2   لغة جافا - الوحدة التاسعةشرح مقرر البرمجة 2   لغة جافا - الوحدة التاسعة
شرح مقرر البرمجة 2 لغة جافا - الوحدة التاسعة
جامعة القدس المفتوحة
 
From Swing to JavaFX
From Swing to JavaFXFrom Swing to JavaFX
From Swing to JavaFX
Yuichi Sakuraba
 
Chap1 1 1
Chap1 1 1Chap1 1 1
Chap1 1 1
Hemo Chella
 
Chap1 1.1
Chap1 1.1Chap1 1.1
Chap1 1.1
Hemo Chella
 
Byte code field report
Byte code field reportByte code field report
Byte code field report
Rafael Winterhalter
 
Java AWT and Java FX
Java AWT and Java FXJava AWT and Java FX
Java AWT and Java FX
pratikkadam78
 
Java programming basics
Java programming basicsJava programming basics
Java programming basics
Hamid Ghorbani
 
Java swing
Java swingJava swing
Java swing
ssuser3a47cb
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Java
yht4ever
 

Similar to SWING.pptx (20)

swings.pptx
swings.pptxswings.pptx
swings.pptx
 
Oop lecture2
Oop lecture2Oop lecture2
Oop lecture2
 
SWING USING JAVA WITH VARIOUS COMPONENTS
SWING USING  JAVA WITH VARIOUS COMPONENTSSWING USING  JAVA WITH VARIOUS COMPONENTS
SWING USING JAVA WITH VARIOUS COMPONENTS
 
GUI Programming with Java
GUI Programming with JavaGUI Programming with Java
GUI Programming with Java
 
Java swing and events
Java swing and eventsJava swing and events
Java swing and events
 
Logic and Coding of Java Interfaces & Swing Applications
Logic and Coding of Java Interfaces & Swing ApplicationsLogic and Coding of Java Interfaces & Swing Applications
Logic and Coding of Java Interfaces & Swing Applications
 
02 basic java programming and operators
02 basic java programming and operators02 basic java programming and operators
02 basic java programming and operators
 
Lecture9 oopj
Lecture9 oopjLecture9 oopj
Lecture9 oopj
 
jframe, jtextarea and jscrollpane
  jframe, jtextarea and jscrollpane  jframe, jtextarea and jscrollpane
jframe, jtextarea and jscrollpane
 
شرح مقرر البرمجة 2 لغة جافا - الوحدة التاسعة
شرح مقرر البرمجة 2   لغة جافا - الوحدة التاسعةشرح مقرر البرمجة 2   لغة جافا - الوحدة التاسعة
شرح مقرر البرمجة 2 لغة جافا - الوحدة التاسعة
 
From Swing to JavaFX
From Swing to JavaFXFrom Swing to JavaFX
From Swing to JavaFX
 
Chap1 1 1
Chap1 1 1Chap1 1 1
Chap1 1 1
 
Chap1 1.1
Chap1 1.1Chap1 1.1
Chap1 1.1
 
Lecture7 oopj
Lecture7 oopjLecture7 oopj
Lecture7 oopj
 
Byte code field report
Byte code field reportByte code field report
Byte code field report
 
Java AWT and Java FX
Java AWT and Java FXJava AWT and Java FX
Java AWT and Java FX
 
Swing
SwingSwing
Swing
 
Java programming basics
Java programming basicsJava programming basics
Java programming basics
 
Java swing
Java swingJava swing
Java swing
 
GUI Programming In Java
GUI Programming In JavaGUI Programming In Java
GUI Programming In Java
 

More from SamyakJain710491

Bayesian Belief Network and its Applications.pptx
Bayesian Belief Network and its Applications.pptxBayesian Belief Network and its Applications.pptx
Bayesian Belief Network and its Applications.pptx
SamyakJain710491
 
Compiler Design PPT.pptx
Compiler Design PPT.pptxCompiler Design PPT.pptx
Compiler Design PPT.pptx
SamyakJain710491
 
performance appraisal at Tech Mahindra.pptx
performance appraisal at Tech Mahindra.pptxperformance appraisal at Tech Mahindra.pptx
performance appraisal at Tech Mahindra.pptx
SamyakJain710491
 
Process Based Estimation.pptx
Process Based Estimation.pptxProcess Based Estimation.pptx
Process Based Estimation.pptx
SamyakJain710491
 
Internet Protocol(Samyak Jain 25014).pptx
Internet Protocol(Samyak Jain 25014).pptxInternet Protocol(Samyak Jain 25014).pptx
Internet Protocol(Samyak Jain 25014).pptx
SamyakJain710491
 
Equivalence of DFAs and NFAs.pptx
Equivalence of DFAs and NFAs.pptxEquivalence of DFAs and NFAs.pptx
Equivalence of DFAs and NFAs.pptx
SamyakJain710491
 
Binomial_Coefficient_and_World_series_problem_using_Dynamic_Programming.pptx
Binomial_Coefficient_and_World_series_problem_using_Dynamic_Programming.pptxBinomial_Coefficient_and_World_series_problem_using_Dynamic_Programming.pptx
Binomial_Coefficient_and_World_series_problem_using_Dynamic_Programming.pptx
SamyakJain710491
 
Cache Coherence.pptx
Cache Coherence.pptxCache Coherence.pptx
Cache Coherence.pptx
SamyakJain710491
 
VALIDATION BASED PROTOCOL AND MULTIPLE GRANULARITY.pptx
VALIDATION BASED PROTOCOL AND MULTIPLE GRANULARITY.pptxVALIDATION BASED PROTOCOL AND MULTIPLE GRANULARITY.pptx
VALIDATION BASED PROTOCOL AND MULTIPLE GRANULARITY.pptx
SamyakJain710491
 
AVL tree PPT.pptx
AVL tree PPT.pptxAVL tree PPT.pptx
AVL tree PPT.pptx
SamyakJain710491
 
Process and Thread Management in Solaris Operating System.pptx
Process and Thread Management in Solaris Operating System.pptxProcess and Thread Management in Solaris Operating System.pptx
Process and Thread Management in Solaris Operating System.pptx
SamyakJain710491
 

More from SamyakJain710491 (11)

Bayesian Belief Network and its Applications.pptx
Bayesian Belief Network and its Applications.pptxBayesian Belief Network and its Applications.pptx
Bayesian Belief Network and its Applications.pptx
 
Compiler Design PPT.pptx
Compiler Design PPT.pptxCompiler Design PPT.pptx
Compiler Design PPT.pptx
 
performance appraisal at Tech Mahindra.pptx
performance appraisal at Tech Mahindra.pptxperformance appraisal at Tech Mahindra.pptx
performance appraisal at Tech Mahindra.pptx
 
Process Based Estimation.pptx
Process Based Estimation.pptxProcess Based Estimation.pptx
Process Based Estimation.pptx
 
Internet Protocol(Samyak Jain 25014).pptx
Internet Protocol(Samyak Jain 25014).pptxInternet Protocol(Samyak Jain 25014).pptx
Internet Protocol(Samyak Jain 25014).pptx
 
Equivalence of DFAs and NFAs.pptx
Equivalence of DFAs and NFAs.pptxEquivalence of DFAs and NFAs.pptx
Equivalence of DFAs and NFAs.pptx
 
Binomial_Coefficient_and_World_series_problem_using_Dynamic_Programming.pptx
Binomial_Coefficient_and_World_series_problem_using_Dynamic_Programming.pptxBinomial_Coefficient_and_World_series_problem_using_Dynamic_Programming.pptx
Binomial_Coefficient_and_World_series_problem_using_Dynamic_Programming.pptx
 
Cache Coherence.pptx
Cache Coherence.pptxCache Coherence.pptx
Cache Coherence.pptx
 
VALIDATION BASED PROTOCOL AND MULTIPLE GRANULARITY.pptx
VALIDATION BASED PROTOCOL AND MULTIPLE GRANULARITY.pptxVALIDATION BASED PROTOCOL AND MULTIPLE GRANULARITY.pptx
VALIDATION BASED PROTOCOL AND MULTIPLE GRANULARITY.pptx
 
AVL tree PPT.pptx
AVL tree PPT.pptxAVL tree PPT.pptx
AVL tree PPT.pptx
 
Process and Thread Management in Solaris Operating System.pptx
Process and Thread Management in Solaris Operating System.pptxProcess and Thread Management in Solaris Operating System.pptx
Process and Thread Management in Solaris Operating System.pptx
 

Recently uploaded

special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
gb193092
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 

Recently uploaded (20)

special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 

SWING.pptx

  • 1. SWING Created by – Samyak Jain (MCA/25014/2022) Abhishek Ahawlat (MCA/25017/2022) submitted to – Santosh Kumar Sir
  • 2. What is Java Swing?  Java Swing is a part of Java Foundation Classes(JFC) that is used to create window-based applications. It is built on top of AWT(Abstract Window Toolkit) API and entirely written in Java.  The javax.swing package provides classes for java swing API such as Jbutton,JTextField,JTextArea,JRadioButton,Jcheckbox ,Jmenu,JColorChooser etc.
  • 4. Components and Containers  The Components class is the superclass of all components.  Swing components are derived from the JComponent class.  A Containers is a special type of component that is designed to hold other components.  The most commonly used containers are JFrame, JPanel and Jwindow etc. Important Method of Component class • Void add(component c) // used to add swing component to a container • Void setSize(int width,int height) //use to set the size of the container • Void setLayout(Layout Manager m) //use to set the Layout • Void setVisible(Boolean b) //to make the frame visible on the window.
  • 6. JFrame • An extension of java.awt.Frame class ,it is the Swing’s version of applet. • JFrame has the option to hide or close the window with the help of setDefaultCloseOperation(int) method. • Example: output : import javax.swing.*; public class NewClass { public static void main(String args[]) { JFrame jf = new JFrame("Swing Example"); jf.setSize(300,300); jf.setVisible(true); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
  • 7. Another way of doing it… import javax.swing.*; public class NewClass extends JFrame { public NewClass() {}; public NewClass(String s) { super(s); } public static void main(String args[]) { NewClass jf = new NewClass("Swing example"); jf.setSize(300,300); jf.setVisible(true); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
  • 8. JTextField The object of a JTextField class is a text component that allows the editing of a single line text. It inherits JTextComponent class. JLabel The object of JLabel class is a component for placing text in a container. It is used to display a single line of read only text. The text can be changed by an application but a user cannot edit it directly. It inherits JComponent class.
  • 9. Swing Example import javax.swing.*; import java.awt.event.*; public class NewClass extends JFrame { JLabel l1,l2,l3,l4; JTextField t1, t2; JButton b1; public NewClass() {}; public NewClass(String s) { super(s); } public void setComponents() { l1 = new JLabel("Addition of two Numbers "); l2 = new JLabel("First Number"); l3 = new JLabel("Second Number"); l4 = new JLabel(); t1 = new JTextField(); t2 = new JTextField();
  • 10. b1 = new JButton("Add"); setLayout(null); l1.setBounds(50,50,200,20); l2.setBounds(50,80,100,20); t1.setBounds(150,80,100,20); l3.setBounds(50,130,100,20); t2.setBounds(150,130,100,20); b1.setBounds(80,180,100,20); l4.setBounds(50,240,200,20); b1.addActionListener(new Handler()); add(l1); add(l2); add(t1); add(l3); add(t2); add(b1); add(l4); }
  • 11. class Handler implements ActionListener { public void actionPerformed(ActionEvent e) { int a = Integer.parseInt(t1.getText()); int b = Integer.parseInt(t2.getText()); int s = a+b; l3.setText("Result is "+s); } } public static void main(String args[]) { NewClass jf = new NewClass("Swing example"); jf.setComponents(); jf.setSize(500,500); jf.setVisible(true); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }