Java Swing Tutorial - Javatpoint
Java Swing Tutorial - Javatpoint
There are many differences between java awt and swing that are given below.
No. Java AWT Java Swing
What is JFC
The Java Foundation Classes (JFC) are a set of GUI components which simplify the development of
desktop applications.
Do You Know
The methods of Component class are widely used in java swing that are given below.
Method Description
https://www.javatpoint.com/java-swing 4/11
22/11/2022, 15:45 Java Swing Tutorial - javatpoint
Let's see a simple swing example where we are creating one button and adding it on the JFrame object
inside the main() method.
File: FirstSwingExample.java
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
}
}
https://www.javatpoint.com/java-swing 5/11
22/11/2022, 15:45 Java Swing Tutorial - javatpoint
We can also write all the codes of creating JFrame, JButton and method call inside the java constructor.
File: Simple.java
import javax.swing.*;
public class Simple {
JFrame f;
Simple(){
f=new JFrame();//creating instance of JFrame
JButton b=new JButton("click");//creating instance of JButton
b.setBounds(130,100,100, 40);
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
}
public static void main(String[] args) {
new Simple();
}
https://www.javatpoint.com/java-swing 6/11
22/11/2022, 15:45 Java Swing Tutorial - javatpoint
}
The setBounds(int xaxis, int yaxis, int width, int height)is used in the above example that sets the
position of the button.
We can also inherit the JFrame class, so there is no need to create the instance of JFrame class
explicitly.
File: Simple2.java
import javax.swing.*;
public class Simple2 extends JFrame{//inheriting JFrame
JFrame f;
Simple2(){
JButton b=new JButton("click");//create button
b.setBounds(130,100,100, 40);
add(b);//adding button on frame
setSize(400,500);
setLayout(null);
setVisible(true);
}
public static void main(String[] args) {
new Simple2();
}}
download this example
What we will learn in Swing Tutorial
JButton class
JRadioButton class
JTextArea class
JComboBox class
JTable class
JColorChooser class
JProgressBar class
JSlider class
Digital Watch
https://www.javatpoint.com/java-swing 7/11