Create a Simple Window Using JFrame

01
of 07

Import the Graphical Components

Import Java Classes
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

 A graphical user interface starts with a top-level container which provides a home for the other components of the interface, and dictates the overall feel of the application. In this tutorial, we introduce the JFrame class, which is used to create a simple top-level window for a Java application.

Open your text editor to start a new text file, and type in the following:

 import java.awt.*;
 import javax.swing.*; 

Java comes with a set of code libraries designed to help programmers quickly create applications. They provide access to classes that perform specific functions, to save you the bother of having to write them yourself. The two import statements above let the compiler know that the application needs access to some of the pre-built functionality contained within the "AWT" and "Swing" code libraries.

AWT stands for “Abstract Window Toolkit.” It contains classes that programmers can use to make graphical components such as buttons, labels and frames. Swing is built on top of AWT, and provides an additional set of more sophisticated graphical interface components. With just two lines of code, we gain access to these graphical components, and can use them in our Java application.

02
of 07

Create the Application Class

Application Class
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

Below the import statements, enter the class definition that will contain our Java application code. Type in:

 //Create a simple GUI window
 public class TopLevelWindow {
 
 } 

All the rest of the code from this tutorial goes between the two curly brackets. The TopLevelWindow class is like the covers of a book; it shows the compiler where to look for the main application code.

03
of 07

Create the Function that Makes the JFrame

Creating the JFrame Object
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

It's good programming style to group sets of similar commands into functions. This design makes the program more readable, and if you want to run the same set of instructions again, all you have to do is run the function. With this in mind, I'm grouping all the Java code that deals with creating the window into one function.

Enter the createWindow function definition:

 private static void createWindow() {
 
 } 

All the code to create the window goes between the function’s curly brackets. Anytime the createWindow function is called, the Java application will create and display a window using this code.

Now, let's look at creating the window using a JFrame object. Type in the following code, remembering to place it between the curly brackets of the createWindow function:

 //Create and set up the window.
 JFrame frame = new JFrame("Simple GUI"); 

What this line does is create a new instance of a JFrame object called "frame". You can think of "frame" as the window for our Java application.

The JFrame class will do most of the work of creating the window for us. It handles the complex task of telling the computer how to draw the window to the screen, and leaves us the fun part of deciding how it's going to look. We can do this by setting its attributes, such as its general appearance, its size, what it contains, and more.

For starters, let's make sure that when the window is closed, the application also stops. Type in:

 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

The JFrame.EXIT_ON_CLOSE constant sets our Java application to terminate when the window is closed.

04
of 07

Add a JLabel to the JFrame

Add a JLabel
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

Since an empty window has little use, let's now put a graphical component inside it. Add the following lines of code to the createWindow function to create a new JLabel object

 JLabel textLabel = new JLabel("I'm a label in the window",SwingConstants.CENTER); textLabel.setPreferredSize(new Dimension(300, 100)); 

A JLabel is a graphical component that can contain an image or text. To keep it simple, it’s filled with the text “I’m a label in the window.” and its size has been set to a width of 300 pixels and height of 100 pixels.

Now that we have created the JLabel, add it to the JFrame:

 frame.getContentPane().add(textLabel, BorderLayout.CENTER); 

The last lines of code for this function are concerned with how the window is displayed. Add the following to ensure that the window appears in the center of the screen:

 //Display the window
 frame.setLocationRelativeTo(null); 

Next, set the window's size:

 frame.pack(); 

The pack() method looks at what the JFrame contains, and automatically sets the size of the window. In this case, it ensures the window is big enough to show the JLabel.

Finally, we need show the window:

 frame.setVisible(true); 

05
of 07

Create the Application Entry Point

All that's left to do is add the Java application entry point. This calls the createWindow() function as soon as the application is run. Type in this function below the final curly bracket of the createWindow() function:

 public static void main(String[] args) {
 
 createWindow();
 
 } 

06
of 07

Check the Code So Far

All the Code for the Application
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

This is a good point to make sure your code matches the example. Here is how your code should look:

 import java.awt.*; 
 import javax.swing.*; 
 
 // Create a simple GUI window
 public class TopLevelWindow {
 
    private static void createWindow() {
 
       //Create and set up the window. 
       JFrame frame = new JFrame("Simple GUI");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
 
       JLabel textLabel = new JLabel("I'm a label in the window",SwingConstants.CENTER); 
       textLabel.setPreferredSize(new Dimension(300, 100)); 
       frame.getContentPane().add(textLabel, BorderLayout.CENTER); 
 
       //Display the window. 
       frame.setLocationRelativeTo(null); 
       frame.pack();
       frame.setVisible(true); 
    }
 
    public static void main(String[] args) {
 
       createWindow();
 
    }
 } 

07
of 07

Save, Compile and Run

Run the Application
Microsoft product screen shot(s) reprinted with permission from Microsoft Corporation.

Save the file as "TopLevelWindow.java".

Compile the application in a terminal window using the Javac compiler. If you’re unsure how to do so, look at the compilation steps from the first Java application tutorial.

javac TopLevelWindow.java

Once the application compiles successfully, run the program:

java TopLevelWindow

After pressing Enter, the window will appear, and you will see your first windowed application.

Well done! this tutorial is the first building block to making powerful user interfaces. Now that you know how to make the container, you can play with adding other graphical components.