Java JFrame Tutorial: Build Your First GUI App Step by Step

Robert Jul 13, 2026

Java JFrame, a part of Swing library, is a powerful tool for creating desktop applications with a graphical user interface (GUI). If you're new to Java or looking to enhance your GUI skills, this tutorial is the perfect starting point. We will guide you through the essentials of JFrame, enabling you to create, design, and manage windows for your Java applications.

Java Cheatsheet
Java Cheatsheet

Whether you're a seasoned Java developer or just starting your journey, understanding JFrame is fundamental to creating interactive and user-friendly software. By the end of this tutorial, you will have hands-on experience with JFrame, equipped with the knowledge to build your own desktop applications.

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

Setting Up and Creating a JFrame

The first step in working with JFrame is setting up the environment and creating a basic window. Let's start with an introduction to JFrame and create our first simple window.

☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀

To begin, import the necessary Java Swing libraries in your class:

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

Creating a JFrame

Javascript window.confirm() method | Javascript tutorial
Javascript window.confirm() method | Javascript tutorial

Now, create a JFrame by initializing it and setting its properties. Here's a simple example:

```java JFrame frame = new JFrame("My First JFrame"); ```

Setting JFrame Properties

ustomize your JFrame by setting its properties, like size, visibility, and default close operation:

the cover of learn java script and arrays in seconds, with text overlaiding it
the cover of learn java script and arrays in seconds, with text overlaiding it

```java frame.setSize(400, 300); // Width and height frame.setVisible(true); // Make the frame visible frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Close when window is closed ```

Designing the JFrame with Components

Now that you have a basic JFrame setup, let's make it more interactive by adding components like buttons, labels, and text fields. We'll use the `add()` method to incorporate these components into our JFrame.

First, add a JLabel for displaying text:

🚀 5 Essential Java Methods Every Developer Should Know! 💻
🚀 5 Essential Java Methods Every Developer Should Know! 💻

```java JLabel label = new JLabel("Welcome to my JFrame!"); frame.add(label); // Add the label to the frame ```

Next, add a JButton with a click event:

```java JButton button = new JButton("Click Me!"); frame.add(button); button.addActionListener(e -> label.setText("Button clicked!")); // Add the button to the frame ```

Using Layout Managers

Learn Java in 30 Days Only
Learn Java in 30 Days Only
Complete Java Cheat Sheet for Beginners to Advanced Developers
Complete Java Cheat Sheet for Beginners to Advanced Developers
How to Become a Java Developer (Step-by-Step)
How to Become a Java Developer (Step-by-Step)
an image of some type of project ideas
an image of some type of project ideas
Master Two Pointers Technique 💻 | Java DSA Interview Preparation Guide
Master Two Pointers Technique 💻 | Java DSA Interview Preparation Guide
the info sheet shows different types of programming
the info sheet shows different types of programming
Javascript String Method
Javascript String Method
Java Project Ideas : -
Java Project Ideas : -
The OOP'S Concept in JAVA🤔💯✅👇
The OOP'S Concept in JAVA🤔💯✅👇
Complete Java Roadmap for Beginners in 2026
Complete Java Roadmap for Beginners in 2026
an image of a computer screen with the text,'java script arrays '
an image of a computer screen with the text,'java script arrays '
Java Project Ideas For Beginners
Java Project Ideas For Beginners
Step - Step Guide To Learn JAVA📖
Step - Step Guide To Learn JAVA📖
Java Project Ideas For Practice 🔥🚀
Java Project Ideas For Practice 🔥🚀
JavaFX Login Example - Scene builder and Netbeans
JavaFX Login Example - Scene builder and Netbeans
Step By Step Guide To Learn Java 👍
Step By Step Guide To Learn Java 👍
Java Roadmap for Beginners 2026
Java Roadmap for Beginners 2026
9 great java project ideas for beginners to use in the web design process
9 great java project ideas for beginners to use in the web design process
JavaScript Manipulando Arrays
JavaScript Manipulando Arrays

Layout managers help organize the components within your JFrame. Try the `FlowLayout` example below:

```java frame.setLayout(new FlowLayout()); // Set the layout manager ```

Positioning and Resizing JFrame

Control the placement and sizing of your frame with methods like `setLocation()`, `pack()`, `setResizable()`, and `setExtendedState()`.

Setting Window Location

Set the location of the JFrame using the `setLocation()` method:

```java frame.setLocation(200, 200); // X and Y coordinates ```

Resizing the JFrame

Use the `pack()` method to fit the frame to its preferred size:

```java frame.pack(); // Resize the frame to fit its contents ```

Preventing Resizing

Prevent the user from resizing the frame using the `setResizable()` method:

```java frame.setResizable(false); // Disable resizing ```

With this knowledge, you can create engaging and dynamic Java desktop applications. If you're ready to take your skills to the next level, explore moreSwing components and APIs for advanced GUI development.

Happy coding! Start experimenting with JFrame today and watch your ideas come to life in desktop applications. Don't forget to share your creations with the community and keep learning to further enhance your Java development skills.