SWING JFrame basics, how to create JFrame

Posted by infocampus on April 25th, 2018

What is JFrame?

JFrame is a class of javax.swing bundle reached out by java.awt.frame; it includes bolster for JFC/SWING part design. It is the best level window, with fringe and a title bar. JFrame class has numerous strategies which can be utilized to alter it.

Making a JFrame

JFrame class has numerous constructors used to make a JFrame. Following is the portrayal.

JFrame (): makes an edge which is imperceptible

JFrame (Graphics Configuration gc): makes an edge with a clear title and illustrations arrangement of screen gadget.

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

JFrame (String title, Graphics Configuration gc): makes a JFrame with particular Graphics setup and determined title.

Here is the simplest example just to make 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);

}

}

Set title of JFrame

To set the title of a JFrame, you can utilize 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);

}

}

Change window size of a JFrame

To resize an edge, JFrame gives a strategy JFrame.setSize(int width, int stature), it takes two parameters width and tallness. Here is what code looks like at this point

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

In the wake of the setting size of a JFrame, you will see you can even now change it measure by essentially putting the cursor at the corners and dragging it. Or then again on the off chance that you press resize choice alongside close at the upper right corner, it will amplify to the measure of full screen. This happens on the grounds that resize is set valid as a matter of course. You can just make false as

Shutting a JFrame

You can undoubtedly close your JFrame by tapping on the X (cross) at the upper left corner of JFrame. However JFrame.setDefaultCloseOperation (int) is a strategy gave by JFrame class, you can set the task that will happen when client taps on a cross. Java Training in BangaloreOn the off chance that "0" is given as a parameter, JFrame won't close even subsequent to tapping on the cross.

The best practice is to utilize JFrame.EXIT_ON_CLOSE; it exits the application (JFrame) and discharges memory.

JFrame.HIDE_ON_CLOSE: It doesn't close JFrame, basically conceals it.

JFrame.DISPOSE_ON_CLOSE: It arranges the edge off, yet it continues running and expands memory.

JFrame.DO_NOTHING_ON_CLOSE: It does nothing when client taps 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);

}

}

Author:

 Java Training in Bangalore-Infocampus

JAVAis one of the best options for the students who want to grow in the future.

Learn Java course in Bangalore We provide Practical Real-Time Training with 100% Placements Assistance.

Attend Free Demo Classes.

Contact: 9740557058

Visit: http://infocampus.co.in/java-training-bangalore.html

 

 

Like it? Share it!


infocampus

About the Author

infocampus
Joined: October 14th, 2015
Articles Posted: 184

More by this author