Creating Frames using Swings in Java
Last Updated :
17 Feb, 2022
Swing is a part of JFC (Java Foundation Classes). Building Graphical User Interface in Java requires the use of Swings. Swing Framework contains a large set of components that allow a high level of customization and provide rich functionalities and is used to create window-based applications.
Java swing components are lightweight, platform-independent, provide powerful components like tables, scroll panels, buttons, lists, color chooser, etc. In this article, we'll see how to make frames using Swings in Java. Ways to create a frame:
Methods:
- By creating the object of Frame class (association)
- By extending Frame class (inheritance)
- Create a frame using Swing inside main()
Way 1: By creating the object of Frame class (association)
In this, we will see how to create a JFrame window by instantiating the JFrame class.
Example:
Java
// Java program to create frames
// using association
import javax.swing.*;
public class test1
{
JFrame frame;
test1()
{
// creating instance of JFrame with name "first way"
frame=new JFrame("first way");
// creates instance of JButton
JButton button = new JButton("let's see");
button.setBounds(200, 150, 90, 50);
// setting close operation
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// adds button in JFrame
frame.add(button);
// sets 500 width and 600 height
frame.setSize(500, 600);
// uses no layout managers
frame.setLayout(null);
// makes the frame visible
frame.setVisible(true);
}
public static void main(String[] args)
{
new test1();
}
}
Way 2: By extending Frame class (inheritance)
In this example, we will be inheriting JFrame class to create JFrame window and hence it won’t be required to create an instance of JFrame class explicitly.
Example:
Java
// Java program to create a
// frame using inheritance().
import javax.swing.*;
// inheriting JFrame
public class test2 extends JFrame
{
JFrame frame;
test2()
{
setTitle("this is also a title");
// create button
JButton button = new JButton("click");
button.setBounds(165, 135, 115, 55);
// adding button on frame
add(button);
// setting close operation
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 500);
setLayout(null);
setVisible(true);
}
public static void main(String[] args)
{
new test2();
}
}
Output:

Note : You won't be able to run this code on an online compiler, so I have added an image to show you the output.
Way 3: Create a frame using Swing inside main()
Example 1:
Java
// Java program to create a frame
// using Swings in main().
import javax.swing.*;
public class Swing_example
{
public static void main(String[] args)
{
// creates instance of JFrame
JFrame frame1 = new JFrame();
// creates instance of JButton
JButton button1 = new JButton("click");
JButton button2 = new JButton("again click");
// x axis, y axis, width, height
button1.setBounds(160, 150 ,80, 80);
button2.setBounds(190, 190, 100, 200);
// adds button1 in Frame1
frame1.add(button1);
// adds button2 in Frame1
frame1.add(button2);
// 400 width and 500 height of frame1
frame1.setSize(400, 500) ;
// uses no layout managers
frame1.setLayout(null);
// makes the frame visible
frame1.setVisible(true);
}
}
Output:

Note: You won't be able to run this code on the online compiler, so I have added an image to show you the output.
Example 2:
Java
// Java program to create a frame
// using Swings in main().
import javax.swing.*;
public class Swing_example_2
{
public static void main(String[] args)
{
// creates instance of JFrame
JFrame frame1 = new JFrame();
// creates instance of JButton
JButton button1 = new JButton("button1");
// "button2" appears on the button
JButton button2 = new JButton("button2");
// x axis, y axis, width, height
button1.setBounds(180, 50, 80, 80);
button2.setBounds(180, 140, 80, 80);
//adds button1 in Frame1
frame1.add(button1);
//adds button2 in Frame1
frame1.add(button2);
//400 width and 500 height of frame1
frame1.setSize(500, 300) ;
//uses no layout managers
frame1.setLayout(null);
//makes the frame visible
frame1.setVisible(true);
}
}
Output:

Note : You won't be able to run this code on an online compiler, so I have added an image to show you the output.
Similar Reads
Java Swing | Internal Frame with examples JInternalFrame is a part of Java Swing . JInternalFrame is a container that provides many features of a frame which includes displaying title, opening, closing, resizing, support for menu bar, etc. Constructors for JInternalFrame JInternalFrame() : creates a new non- closable, non- resizable, non- i
8 min read
Large Icons using javax.swing in Java Large icons generally refer to the set of icons provided by the look and feel of the user interface. The "large icons" provided by the look and feel are typically larger than the standard size of icons used in the interface and are intended to be used in places where a larger, more visually prominen
3 min read
Student Grade Calculator using Java Swing Consider the following scenario. We need to create a GUI based application which calculates grade of students according to marks obtained in 4 subjects. Below are the range of marks for different grades. MarksGrade>90%ABetween 85% - 90%BBetween 80% - 85%CBetween 70% - 80%DBetween 60% - 70%EBetwee
4 min read
Java Swing | Creating a Toast Message What are toast messages? And how to create them by using Java Swing? Toast Messages are a quick way of informing the user by short Pop-up messages that last for a short period of time and then disappear. Java Swing does not have an inbuilt class for toast message but toast message is a popular and a
3 min read
How to Use Swing Applet in Java? In this article, we will be using the swing Applet or JApplet in Java. Here, we will make a simple multiplication application that will multiply the two input numbers. Approach to Using Swing Applet in JavaWe need to import the packages for Swing and AWT Components.Once the packages are imported, we
4 min read