Java JFrame Example Code Tutorial: Build Your First GUI App

Robert Jul 13, 2026

Java JFrame, a graphical user interface (GUI) component, is a versatile tool for creating standalone desktop applications. Understanding how to code with Java JFrame is essential for any Java developer looking to create user-friendly applications. Let's explore how to create a simple JFrame application, including a button, and react to a click event.

Java Cheatsheet
Java Cheatsheet

A JFrame is a container in the Java Abstract Window Toolkit (AWT) and Java Foundation Classes (JFC) that enables us to create a basic window. Here's a basic example to get us started:

Java Collection Framework: ArrayList Explained Visually
Java Collection Framework: ArrayList Explained Visually

Setting Up a Simple Java JFrame

The first step is to import the necessary classes and create a new `JFrame`. Below is a basic setup:

30 Days of Java – Day 17 | Java Interfaces Explained for Beginners
30 Days of Java – Day 17 | Java Interfaces Explained for Beginners


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

public class SimpleFrame extends JFrame {
    public SimpleFrame() {
        setSize(300, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        SimpleFrame frame = new SimpleFrame();
    }
}

Setting the Frame's Properties

In the code above, `setSize(300, 200)` sets the frame's size, and `setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)` ensures the application exits when the frame is closed. The `setVisible(true)` call makes the frame appear on the screen.

30 Days of Java – Day 7 | Java Arrays Explained for Beginners
30 Days of Java – Day 7 | Java Arrays Explained for Beginners

Creating a Basic Button

Now, let's add a `JButton` to our frame. We'll also make the button perform an action when clicked:

Adding a JButton and Listening for Clicks

Logical Operators in Java: Think Smarter, Code Better ☕🚀
Logical Operators in Java: Think Smarter, Code Better ☕🚀

We can create a `JButton` and add it to our `JFrame` using the `add` method. We'll also create an `ActionListener` to handle the click event:


import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ClickableFrame extends JFrame {
    public ClickableFrame() {
        JButton button = new JButton("Click me!");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "Button clicked!");
            }
        });
        add(button);
        setSize(300, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        ClickableFrame frame = new ClickableFrame();
    }
}

Handling the Click Event

In the code above, `JOptionPane.showMessageDialog(null, "Button clicked!")` displays a dialog box with the message "Button clicked!" when the button is clicked.

JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)

Improving the GUI with Layout Managers

Java provides several layout managers that help arrange components within a container. Let's use the `FlowLayout` to center our button in the frame:

Java if...else (With Examples)
Java if...else (With Examples)
JavaScript Basics | Chapter 1 📖 | Part 1
JavaScript Basics | Chapter 1 📖 | Part 1
the different types of html tags are shown in this screenshote screen graber
the different types of html tags are shown in this screenshote screen graber
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
Java Cheat Sheet: Operators, Scanner & User Input ☕
Java Cheat Sheet: Operators, Scanner & User Input ☕
Promises
Promises
Title: 🎯 Switch Case in Java Simplified!
Title: 🎯 Switch Case in Java Simplified!
Learn Java Collections Fast 🚀 | ArrayList HashMap Set Queue Explained
Learn Java Collections Fast 🚀 | ArrayList HashMap Set Queue Explained
Java Patterns Notes 1
Java Patterns Notes 1
Java Conditional Statements Cheat Sheet
Java Conditional Statements Cheat Sheet
Top Features of Java Every Beginner Should Know ☕🚀
Top Features of Java Every Beginner Should Know ☕🚀
The OOP'S Concept in JAVA🤔💯✅👇
The OOP'S Concept in JAVA🤔💯✅👇
Javascript String Methods
Javascript String Methods
an info sheet with different types of web pages
an info sheet with different types of web pages
an image of a computer screen with the text,'java script arrays '
an image of a computer screen with the text,'java script arrays '
JavaFX Login Example - Scene builder and Netbeans
JavaFX Login Example - Scene builder and Netbeans
Java script
Java script
✅ Java Inheritance Example Explained Simply
✅ Java Inheritance Example Explained Simply
Java script string method
Java script string method
Master Two Pointers Technique 💻 | Java DSA Interview Preparation Guide
Master Two Pointers Technique 💻 | Java DSA Interview Preparation Guide

Using FlowLayout

We can set the layout of our `JFrame` using `setLayout(new FlowLayout())`. This centers the button in the middle of the frame:

With this, you have a basic understanding of how to work with Java JFrame. You can now start building more complex user interfaces and interact with the user through GUI components.

Don't forget to practice and experiment with other layout managers, GUI components, and event handling mechanisms to improve your proficiency with Java JFrame.