SWING JFrame basics, how to create JFrame

This tutorial explains JFrame basics from creation to customization.

What is JFrame?

JFrame is a class of javax.swing package extended by java.awt.frame, it adds support for JFC/SWING component architecture. It is the top level window, with border and a title bar. JFrame class has many methods which can be used to customize it.

Creating a JFrame

JFrame class has many constructors used to create a JFrame. Following is the description.

JFrame(): creates a frame which is invisible

JFrame(GraphicsConfiguration gc): creates a frame with a blank title and graphics configuration of screen device.

JFrame(String title): creates a JFrame with a title.

JFrame(String title, GraphicsConfiguration gc): creates a JFrame with specific Graphics configuration and specified title.




Here is a simplest example just to create a JFrame.

package Example;

import java.awt.GraphicsConfiguration;

import javax.swing.JFrame;

public class JFrameExample {
	
	static GraphicsConfiguration gc;
	public static void main(String[] args){
		JFrame frame= new JFrame(gc);	
		frame.setVisible(true);
	}
}

Here is how it will display

Simple JFrame window

JFrame

Set title of JFrame

To set title of a JFrame, you can use JFrame.setTitle(String title).

Here is the code

package Example;

import java.awt.GraphicsConfiguration;

import javax.swing.JFrame;

public class JFrameExample {
	
	static GraphicsConfiguration gc;
	public static void main(String[] args){
		JFrame frame= new JFrame(gc);	
		frame.setTitle("Welecome to JavaTutorial.net");
		frame.setVisible(true);
	}
}

Here how it looks

Set title of a JFrame

Set title of a JFrame

Change window size of a JFrame

To resize a frame, JFrame provides a method JFrame.setSize(int width, int height), it takes two parameters width and height. Here is how code looks now

package Example;

import java.awt.GraphicsConfiguration;

import javax.swing.JFrame;

public class JFrameExample {
	
	static GraphicsConfiguration gc;
	public static void main(String[] args){
		JFrame frame= new JFrame(gc);	
		frame.setTitle("Welecome to JavaTutorial.net");
		frame.setSize(600, 400);
		frame.setVisible(true);
	}
}

Resize a JFrame

After setting size of a JFrame you will notice you can still change it size by just simply putting the cursor at the corners and dragging it. Or if you press resize option next to close at the top right corner, it will maximize to the size of full screen. This happens because resize is set true by default. You can simply make false as

JFrame.setResizable(false), now it will appear according to the dimensions you have given in code and will not resize by the graphical interface.

Change position on the screen

To change position of JFrame on screen JFranme provides a method JFrame.setlocation(int x, int), it takes two paramters x represents position along x-axis and y represents position along y-axis. The top left corner of your screen is (0,0).

Closing a JFrame

You can easily close your JFrame by clicking on the X(cross) at the top left corner of JFrame. However JFrame.setDefaultCloseOperation(int) is a method provided by JFrmae class, you can set the operation that will happen when user clicks on cross. If “0” is given as a parameter, JFrame will not close even after clicking on cross.

The best practice is to use JFrame.EXIT_ON_CLOSE, it exits application (JFrame) and releases memory.

JFrame.HIDE_ON_CLOSE: It doesnot close JFrame, simply hides it.

JFrame.DISPOSE_ON_CLOSE: It dispose the frame off, but it keeps running and consumes memory.

JFrame.DO_NOTHING_ON_CLOSE: It does nothing when user clicks on close.

Here’s how the final code looks like

package Example;

import java.awt.GraphicsConfiguration;

import javax.swing.JFrame;

public class JFrameExample {
	
	static GraphicsConfiguration gc;
	public static void main(String[] args){
		JFrame frame= new JFrame(gc);	
		frame.setTitle("Welecome to JavaTutorial.net");
		frame.setSize(600, 400);
		frame.setLocation(200, 200);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
	}
}

You can download the source code from this link

4.4 8 votes
Article Rating
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
chadiria
chadiria
1 year ago

package Example;import java.awt.GraphicsConfiguration;import javax.swing.JFrame;public class JFrameExample { static GraphicsConfiguration gc; public static void main(String[] args){ JFrame frame= new JFrame(gc); frame.setTitle(“Welecome to JavaTutorial.net”); frame.setSize(600, 400); frame.setLocation(200, 200); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); }}

Aacem Rick
Aacem Rick
1 year ago

Hey the tuturials are very nice. thanks for ur time