Java JFrame
Last Updated : 17 Nov, 2023
Thе Java JFramе is an еssеntial componеnt of Java Swing, which is a part
of thе Java SWT(Standard Widgеt Toolkit). JFrame in Java is a class that
allows you to crеatе and manage a top-lеvеl window in a Java application.
It sеrvеs as thе main window for GUI-basеd Java applications and
providеs a platform-indеpеndеnt way to crеatе graphical usеr intеrfacеs.
In Java JFrame is a part of javax.swing package. In this article, we will
learn about Java JFrame.
Constructor of Java JFrame
Constructor Description
This is the default constructor
JFrame() for JFrame. It creates a new
frame with no title
This constructor creates a new
JFrame(String title)
frame with the specified title.
This constructor creates a
JFrame(GraphicsConfiguration gc) JFrame that uses the specified
graphics configuration.
This constructor creates a
JFrame(String title, JFrame with the specified title
GraphicsConfiguration gc) and using the specified
graphics configuration.
This constructor creates a
JFrame(Rectangle bounds) JFrame with the specified
bounds.
This constructor creates a
JFrame(String title, Rectangle
JFrame with the specified title
bounds)
and bounds.
Methods of Java JFrame
Methods Description
setTitle(String title) Sets the title of the JFrame.
setSize(int width, int height) Sets the size of the JFrame.
Sets the default close operation for
the JFrame. Common options include
setDefaultCloseOperation(int
JFrame.EXIT_ON_CLOSE,
operation)
JFrame.HIDE_ON_CLOSE, and
JFrame.DO_NOTHING_ON_CLOSE.
Sets the visibility of the JFrame. Pass
setVisible(boolean b) true to make it visible and false to
hide it.
Sets the layout manager for the
setLayout(LayoutManager JFrame, which controls how
manager) components are arranged within the
frame.
Adds a Swing component to the
add(Component comp)
JFrame.
Removes a component from the
remove(Component comp)
JFrame.
Forces the layout manager to
validate() recalculate the layout of components
within the JFrame.
Controls whether the user can resize
setResizable(boolean resizable)
the JFrame.
Sets the icon (image) for the JFrame
setIconImage(Image image)
window.
Parent Classes of JFrame Methods
Java JFrame is the part of Java Swing and the classes from where all the
methods associated with JFrame are inherited are mentioned below:
1. java.awt.Frame
2. java.awt.Container
3. java.awt.Window
4. javax.swing.JFrame
Some Fields for Java JFrame
Fields Description
A field that specifies the default
EXIT_ON_CLOSE (int) operation to perform when the
user closes the JFrame. It’s
typically used with the
setDefaultCloseOperation()
method.
It is constant that specifies an
operation to perform when the
DISPOSE_ON_CLOSE (int) user closes the JFrame.It disposes
of the JFrame but doesn’t exit the
application.
Specifies that the JFrame should
HIDE_ON_CLOSE (int) be hidden but not disposed of
when the user closes it.
Indicates that no action should be
DO_NOTHING_ON_CLOSE (int) taken when the user closes the
JFrame.
Programs to implement JFrame
1. Java Program to demonstrate a simple JFrame
Below is the demonstration of the simple JFrame:
Java
// Java Swing Program to demonstrate
// a simple JFrame
import javax.swing.JFrame;
import javax.swing.JLabel;
// Driver Class
public class MyJFrame {
// main function
public static void main(String[] args)
// Create a new JFrame
JFrame frame = new JFrame("My First JFrame");
// Create a label
JLabel label
= new JLabel("Geeks Premier League 2023");
// Add the label to the frame
frame.add(label);
// Set frame properties
frame.setSize(300,
200); // Set the size of the frame
// Close operation
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
// Make the frame visible
frame.setVisible(true);