Home » Desktop Java » swing » JFrame » Java JFrame Example

About Konstantina Dimtsa

Konstantina Dimtsa
Konstantina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she is currently pursuing M.Sc studies in Advanced Information Systems at the same department. She is also working as a research associate for NKUA in the field of telecommunications. Her main interests lie in software engineering, web applications, databases and telecommunications.

Java JFrame Example

In this example we will show how to use the class JFrame and how to create a simple gui by using the javax.swing package. JFrame class is one of the containers provided by the java.awt package. Containers in java.awt are components where other components can be located. Another example of a container provided by java.awt is JPanel. So, JFrame works like the main window where your components (e.g. labels, buttons, textfields etc) are added so as to create a GUI. Of course, Java provides many methods so as to decorate the frame as you wish.
 
 
 
 

1. Example of JFrame

Create a java class named TestFrameExample.java with the following code:

TestFrameExample.java

package com.javacodegeeks.desktopjava.swing.jframe;

import java.awt.FlowLayout;

import javax.swing.*;

public class TestFrameExample {

	public static void main(String s[]) {

		JFrame frame = new JFrame("JFrame Example");

		JPanel panel = new JPanel();
		panel.setLayout(new FlowLayout());

		JLabel label = new JLabel("This is a label!");

		JButton button = new JButton();
		button.setText("Press me");

		panel.add(label);
		panel.add(button);

		frame.add(panel);
		frame.setSize(300, 300);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);

	}

}

Let’s explain the above code. Firstly, we create a frame by using a single argument constructor of the JFrame where we declare the title of the window of the frame. Next, we create a JPanel, which is another type of container. In this panel, we are going to add other components and the panel itself will be added to the frame. In this case, the usage of JPanel is not necessary as we have only one panel, so we could add the other components directly to the frame. However, in other cases, you may need to use more than one panel in the same frame, which will be switched and will have different components in them. In the panel we declared, we declare also the layout of the included components. In this case, we use the FlowLayout layout which adds the components to the container in left-to-right order. Next, we create two components,a label and a button, that are added to the panel.

Now, we will see the declaration of a frame. Firstly, we add the above panel to the frame and then, we declare the size of the window of the frame. By default, a frame is displayed in the upper-left corner of the screen, so if we need to display a frame in another position, we can use the setLocation(x, y) or the setLocationRelativeTo(component) method. In this case, we use the setLocationRelativeTo(null) which places the frame to the center of the screen. Then, we would like to define the action of the frame when we press the close button. The close button of the frame by default performs the hide operation for the JFrame. In this example we have changed this behavior to window close operation by setting the setDefaultCloseOperation() to EXIT_ON_CLOSE value. Finally, we make the frame visible by using the setVisible(true) method, as the frame initially is not visible.

If we run the above code, we will see the following window:

JFrame Example

JFrame Example

2. Download the source code

This was an example of how to use the class JFrame. You can download the Eclipse project of this tutorial here: JFrameExample.zip

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

 

Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns