JFrame in Java Swing

JFrame is a class in the Java Swing library. It is used to create a windowed graphical user interface (GUI) in a Java program. It provides a framework for building graphical user interfaces by providing methods for creating and manipulating various graphical components such as buttons, labels, text fields, and other controls.

JFrame is a top-level container that serves as the main window of a Java GUI application. It has a title bar, a border, and can be resized, maximized, minimized, and closed by the user. We can add various other components to the JFrame, such as buttons, text fields, labels, and menus.

To create a JFrame, we first create an instance of the class, set its properties such as title and size, add other components to it, and then display it using the setVisible() method.

Following is an example code that creates a JFrame and displays a GUI as an output.

import javax.swing.JFrame;

public class JFrameDemo {
	public static void main(String[] args) {
		JFrame frame = new JFrame("Hello World"); // create a new JFrame
		frame.setSize(300, 200); // set its size
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // set default close operation
		frame.setVisible(true); // show the JFrame
	}
}

Output:

JFrame in Java Swing