Java/J2EE and SOA (325 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (41 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (324 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

How to Implement a JFrame Class in Java

Published on Oct 15,2019 44 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

JFrame Class in Java is an important aspect of the Graphical User Interface. The following pointers will be discussed in this article.

 

Introduction

The class JFrame is basically an extended version of Java.awt.Frame or we can also state that the javax.swing.JFrame class is a type of container inheriting the java.awt.Frame class.
Whenever a Graphical Use Interface (GUI) is created with Java Swing functionality, a container is required where components like labels, buttons, textfields are added to create a Graphical User Interface(GUI) and is known as JFrame.
JFrame has its own methods as well as constructors just like a Class.
Methods are functions impacts the properties of JFrame including its size or visibility and Constructors are run after once the instance is created.

Note: Importing Java Swing interface is mandatory to use this class :- import javax.swing.*;

 

 

Creating A JFrame in Java

To create a JFrame, we have to create the instance of JFrame class. We have many constructors to create a JFrame.

  • JFrame(): It creates a frame but is invisible
  • JFrame(GraphicsConfiguration gc): It creates a frame having a blank title and graphics configuration of the screen of device.
  • JFrame(String title): It creates a JFrame having a title.
  • JFrame(String title, GraphicsConfiguration gc): It creates a JFrame with specific Graphics configuration as well as a specified title.

Code to create a JFrame in Java:


package ExampleNumber1;
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);
}
}

Output:

Output- JFrame Class in Java - Edureka

Lets understand the change window size of JFrame!

Change window Size of a JFrame

To resize a frame, There is a method JFrame.setSize(int width, int height) which takes two parameters width and height. Below is the code to change the window size of a JFrame.




package ExampleNumber2;
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("Hello, My name is Yashwinder");
frame.setSize(600, 400);
frame.setVisible(true);
}
}

Lets move on with Resizing a JFrame.

 

 

Resizing a JFrame in Java

After setting a certain size of a JFrame it is observed that we can still change the size by just simply hovering the cursor at the corners and dragging it as per the size requirements. If the resize option is pressed which is next to close at the top right corner, it will maximize to the size of full screen. This generally happens because resize is set true by default. You can also make false as
JFrame.setResizable(false) – It will appear according to the dimensions you have given in code and now we will not be able to resize the JFrame by the graphical User interface (GUI).

Lets understand Changing position on the screen.

Changing position on the screen

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

Lets move on to closing a JFrame.

 

Closing a JFrame in Java

We can easily close your JFrame by clicking on the X(cross) button easily available at the top left corner of JFrame. However JFrame.setDefaultCloseOperation(int) is a method provided by JFrame class. you can set the operation that will happen when the user clicks on the cross. If in any case “0” is passed as a parameter, JFrame will never close even after clicking on the cross.
The best way to close a JFrame is to use JFrame.EXIT_ON_CLOSE – exits the application (JFrame) and releases the used memory.
JFrame.HIDE_ON_CLOSE – doesn’t close JFrame. It simply hides it.
JFrame.DISPOSE_ON_CLOSE- disposes the frame off but it keeps running. It also consumes memory.
JFrame.DO_NOTHING_ON_CLOSE- does nothing at the time when the user clicks on close.

Example:
Below are the two simple examples of creating a JFrame with given dimensions, resize and no resize properties, and setting title of JFrame etc.

import java.awt.*;
import javax.swing.*;
public class JFrameExam implements Runn
{
public static void main(String[] args)
{
JFrameExample example = new JFrameExample();
// schedule this for the event dispatch thread (edt)
SwingUtilities.invokeLater(example);
}

public void run()
{
JFrame frame = new JFrame("My First JFrame ExampleNumber 3 ");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(400, 200));
frame.pack();
frame.setVisible(true);
}
}

package ExampleNumber4;
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("Hello, my name is Yash");
frame.setSize(600, 400);
frame.setLocation(200, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
}

How to create, center and display a JFrame

Code:


import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

// A sample class demonstrating to create and display a JFrame.

public class SimpleJFrame
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
// creating a jframe, giving it an initial title
JFrame frame = new JFrame("First JFrame Demo Here");

// set the jframe size. in a more complicated application you will
// probably call frame.pack() before displaying it.
frame.setSize(new Dimension(300,200));

// center the frame
frame.setLocationRelativeTo(null);

// set this so the application can easily be stopped/Ended.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// displaying the frame
frame.setVisible(true);
}
});
}
}

Moving on with Explanation of the code.

Explanation of the code:

The Execution starts in the main method of the SimpleJFrame Java class.

There are the SwingUtilities invokeLater method used as a wrapper around most of code. Unfortunately this is the most complicated line of code, and it comes first, but in general this technique is used to make sure that it execute Swing code like this on the Event Dispatch Thread, or EDT.

The frame.setLocationRelativeTo(null) is a slightly-special way of centering the JFrame on screen. It’s actually discussed in the JFrame Javadoc, but when you first start working with Swing and JFrame’s this isn’t obvious at all. The frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) method sets up the application so that when the user presses the close button on the window they see on the top left corner, the entire application will shut down. This technique is fine for simple applications like this example, but with more complicated applications you’ll want to control that shutdown process more carefully

 

Summary

JFrame is a class in Java and has its own methods and constructors. With this, we come to the end of this JFrame Class in Java article. Check out the Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer.

Got a question for us? Please mention it in the comments section of this “JFrame CLass in Java” blog  and we will get back to you as soon as possible.

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.