Site icon TestingDocs.com

Create a Swing Frame window

Overview

In this tutorial, we will learn how to create a Swing Frame window. A frame is a window with a title and a border.

Swing Frame

We can create a frame using the JFrame class. JFrame is a top-level Swing container that provides a place for other Swing GUI components to paint themselves.  We need to import the class using the below statement:

import javax.swing.JFrame;

Java Program

package com.testingdocs.swing.components;

/**********************************************
 * FileName: JFrameDemo.java
 * Package : com.testingdocs.swing.components
 * 
 * Java Tutorials - www.TestingDocs.com
 **********************************************/

import javax.swing.JFrame;

public class JFrameDemo {
	public static void main(String[] args) {
		// Create JFrame 
		JFrame frame = new JFrame("JFrameDemo - www.TestingDocs.com");
		//Frame properties
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(600, 400);
		frame.setVisible(true);
	}
}

Output

Run the Java application to view the output.

 

JFrame frame = new JFrame(“JFrameDemo – www.TestingDocs.com”);

Create a frame with title the ‘FrameDemo – www.TestingDocs.com’

frame.setSize(600, 400);

Set the size for the window. The width and the height of the window.

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

Make the application quit when the close box is clicked

frame.setVisible(true);

The setVisible will display the windows on the screen. This call also starts a separate thread to monitor user interaction with the window user interface. Even if the main method returns, the execution continues because the setVisible() call creates another execution thread.

The frame window can be resized and dragged. Users can also minimize, maximize or close the window.

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/in/java/

Exit mobile version