Java Swing
S.Kavitha
Head & Assistant Professor
Department of Computer Science
Sri Sarada Niketan College of Science for
Women,Karur.
• Java Swing tutorial is a part of Java
Foundation Classes (JFC) that is used to create
window-based applications.
• It is built on the top of AWT (Abstract
Windowing 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.
What is JFC
• The Java Foundation Classes (JFC) are a set of
GUI components which simplify the
development of desktop applications.
Hierarchy of Java Swing classes
Java Swing Examples
There are two ways to create a frame:
• By creating the object of Frame class
(association)
• By extending Frame class (inheritance)
• We can write the code of swing inside the
main(), constructor or any other method.
Simple Java Swing Example
import javax.swing.*;
public class FirstSwingExample {
public static void main(String[] args) {
JFrame f=new JFrame();//creating instance of JFrame
JButton b=new JButton("click");//creating instance of JButton
b.setBounds(130,100,100, 40);//x axis, y axis, width, height
f.add(b);//adding button in JFrame
f.setSize(400,500);//400 width and 500 height
f.setLayout(null);//using no layout managers
f.setVisible(true);//making the frame visible
}
}
output