---
description: Java Swing package lets you make GUI components for your java applications. This tutorial gives programs and examples to create Swing GUI.
title: "Java Swing Tutorial: How to Create a GUI Application in Java"
image: https://www.guru99.com/images/java-swing-tutorial.png
---

[Skip to content](#main)

**⚡ Smart Summary**

Java Swing Tutorial covers the lightweight GUI toolkit for desktop apps with containers, components, and layouts.

- 🪟 **Pure Java:** Same look across OSes.
- 🏗️ **Containers:** JFrame hosts UI.
- 🧱 **Components:** JButton and JLabel cover UIs.
- 📐 **Layouts:** BorderLayout and GridBagLayout.
- 🆚 **Swing vs JavaFX:** Swing is frozen; JavaFX is modern.
- 🤖 **AI Migration:** AI migrates Swing to JavaFX.

Read More

![Java Swing GUI Tutorial](https://www.guru99.com/images/java-swing-tutorial.png)

## What is Swing in Java?

**Swing in Java** is a Graphical User Interface (GUI) toolkit that includes the GUI components. Swing provides a rich set of widgets and packages to make sophisticated GUI components for Java applications. Swing is a part of Java Foundation Classes(JFC), which is an API for Java GUI programing that provide GUI.

The Java Swing library is built on top of the Java Abstract Widget Toolkit (**AWT**), an older, platform dependent GUI toolkit. You can use the Java simple GUI programming components like button, textbox, etc., from the library and do not have to create the components from scratch.

**In this Java Swing tutorial, you will learn Java GUI basics like-**

### Java Swing class Hierarchy Diagram

[![Java Swing Class Hierarchy Diagram]()](https://www.guru99.com/images/uploads/2012/06/java-swing-class-hierarchy.jpg)

*Java Swing Class Hierarchy Diagram*

All components in Java Swing are JComponent which can be added to container classes.

## What is a Container Class?

Container classes are classes that can have other components on it. So for creating a Java Swing GUI, we need at least one container object. There are 3 types of Java Swing containers.

1. **Panel**: It is a pure container and is not a window in itself. The sole purpose of a Panel is to organize the components on to a window.
2. **Frame**: It is a fully functioning window with its title and icons.
3. **Dialog**: It can be thought of like a pop-up window that pops out when a message has to be displayed. It is not a fully functioning window like the Frame.

## What is GUI in Java?

**GUI (Graphical User Interface) in Java** is an easy-to-use visual experience builder for Java applications. It is mainly made of graphical components like buttons, labels, windows, etc. through which the user can interact with an application. GUI plays an important role to build easy interfaces for Java applications.

## How to Make a GUI in Java with Example

Now in this Java GUI Tutorial, let’s understand how to create a GUI in Java with Swing in Java examples.

**Step 1)** Copy code into an editor  
 In first step Copy the following code into an editor.

```
import javax.swing.*;
class gui{
    public static void main(String args[]){
       JFrame frame = new JFrame("My First GUI");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setSize(300,300);
       JButton button = new JButton("Press");
       frame.getContentPane().add(button); // Adds Button to content pane of frame
       frame.setVisible(true);
    }
}

```

**Step 2)** Run the code  
 Next step, Save, Compile, and Run the code

**Step 3)** Copy following code into an editor  
 Now let’s Add a Button to our frame. Copy following code into an editor from given Java UI Example

```
import javax.swing.*;
   class gui{
      public static void main(String args[]){
        JFrame frame = new JFrame("My First GUI");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300,300);
       JButton button1 = new JButton("Press");
       frame.getContentPane().add(button1);
       frame.setVisible(true);
     }
}

```

**Step 4)** Execute the code  
 Next, Execute the code. You will get a big button.

[  
![Java GUI Example]()](https://www.guru99.com/images/uploads/2012/06/java-sswing-button.jpg)

**Step 5)** Add two buttons

How about adding two buttons? Copy the following code into an editor.

```
import javax.swing.*;
class gui{
      public static void main(String args[]){
           JFrame frame = new JFrame("My First GUI");
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           frame.setSize(300,300);
          JButton button1 = new JButton("Button 1");
          JButton button2 = new JButton("Button 2");
          frame.getContentPane().add(button1);
          frame.getContentPane().add(button2);
          frame.setVisible(true);
     }
}

```

**Step 6)** Save & Run the program  
 Next, Save, Compile, and Run the program.

**Step 7)** Check output  
 Unexpected output =? Buttons are getting overlapped.

**Don't Miss:**

- [How to Download & Install Java JDK 8 in Windows](https://www.guru99.com/install-java.html)
- [HashMap in Java](https://www.guru99.com/hashmap-in-java.html)
- [Top 30 Struts Interview Questions and Answers (2026)](https://www.guru99.com/struts-interview-questions.html)
- [Top 30 Log4j Interview Questions and Answers (2026)](https://www.guru99.com/log4j-interview-questions.html)

## Java Layout Manager

The Layout manager is used to layout (or arrange) the GUI Java components inside a container. There are many layout managers, but the most frequently used are-

## Java BorderLayout

A `BorderLayout` places components in up to five areas: top, bottom, left, right, and center. It is the default layout manager for every java JFrame

[![Java BorderLayout]()](https://www.guru99.com/images/uploads/2012/06/java-border-layout-manager.jpg)

*Java BorderLayout*

## Java FlowLayout

`FlowLayout` is the default layout manager for every `JPanel`. It simply lays out components in a single row one after the other.

[![Java FlowLayout]()](https://www.guru99.com/images/uploads/2012/06/java-flow-layout-manager.jpg)

*Java FlowLayout*

## Java GridBagLayout

It is the more sophisticated of all layouts. It aligns components by placing them within a grid of cells, allowing components to span more than one cell.

[![Java GridBagLayout]()](https://www.guru99.com/images/uploads/2012/06/java-grid-bag-layout.jpg)

*Java GridBagLayout*

**Step 8)** Create chat frame  
 How about creating a chat frame like below?

[  
![Example of Java GUI]()](https://www.guru99.com/images/uploads/2012/06/java-swing-chat-frame.jpg)

Try to code yourself before looking at the program below.

```
//Usually you will require both swing and awt packages
// even if you are working with just swings.
import javax.swing.*;
import java.awt.*;
class gui {
    public static void main(String args[]) {

        //Creating the Frame
        JFrame frame = new JFrame("Chat Frame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);

        //Creating the MenuBar and adding components
        JMenuBar mb = new JMenuBar();
        JMenu m1 = new JMenu("FILE");
        JMenu m2 = new JMenu("Help");
        mb.add(m1);
        mb.add(m2);
        JMenuItem m11 = new JMenuItem("Open");
        JMenuItem m22 = new JMenuItem("Save as");
        m1.add(m11);
        m1.add(m22);

        //Creating the panel at bottom and adding components
        JPanel panel = new JPanel(); // the panel is not visible in output
        JLabel label = new JLabel("Enter Text");
        JTextField tf = new JTextField(10); // accepts upto 10 characters
        JButton send = new JButton("Send");
        JButton reset = new JButton("Reset");
        panel.add(label); // Components Added using Flow Layout
        panel.add(tf);
        panel.add(send);
        panel.add(reset);

        // Text Area at the Center
        JTextArea ta = new JTextArea();

        //Adding Components to the frame.
        frame.getContentPane().add(BorderLayout.SOUTH, panel);
        frame.getContentPane().add(BorderLayout.NORTH, mb);
        frame.getContentPane().add(BorderLayout.CENTER, ta);
        frame.setVisible(true);
    }
}
```

## FAQs

⚡ Difference between AWT, Swing, and JavaFX?

AWT uses native widgets. Swing is pure Java. JavaFX is modern.

🤖 Can AI convert Swing apps to JavaFX?

Yes. Copilot translates components.

💡 How does AI help Swing developers?

AI generates JFrame code; flags EDT issues.

🪟 Is Swing recommended for new desktop apps?

Oracle recommends JavaFX. Swing for legacy.

🏗️ Top-level container in Swing?

JFrame is the standard window.

📐 Most flexible Swing layout?

GridBagLayout is most flexible.

🛡️ Why run Swing code on the EDT?

Swing is not thread safe; use SwingUtilities.invokeLater.

📚 How to change Swing look and feel?

Use UIManager.setLookAndFeel.

#### Summarize this post with:

ChatGPTPerplexityGrokGoogle AI

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.**

##### Sign up for the newsletter

Subscribe for Free

You have successfully subscribed.  
Please check your inbox.

![AI-Newsletter]() Chosen by over **350,000+** professionals

[Scroll to topScroll to top](#wrapper) Scroll to top

×

Toggle Menu Close

Search for:

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/java-swing-tutorial.png","url":"https://www.guru99.com/images/java-swing-tutorial.png","width":"700","height":"250","caption":"Java Swing Tutorial","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/java-swing-gui.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/java-tutorials","name":"Java Tutorials"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/java-swing-gui.html","name":"Java Swing Tutorial: How to Create a GUI Application in Java"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/java-swing-gui.html#webpage","url":"https://www.guru99.com/java-swing-gui.html","name":"Java Swing Tutorial: How to Create a GUI Application in Java","dateModified":"2026-06-23T16:23:40+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/java-swing-tutorial.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/java-swing-gui.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/james","name":"James Hartman","description":"I am James Hartman, a seasoned professional in Oracle Certified Java Professional tutorials, specializing in crafting comprehensive guides to help you excel in your Java certification journey.","url":"https://www.guru99.com/author/james","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/james-hartman-author-v2-120x120.png","url":"https://www.guru99.com/images/james-hartman-author-v2-120x120.png","caption":"James Hartman","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"Java Tutorials","headline":"Java Swing Tutorial: How to Create a GUI Application in Java","description":"Java Swing package lets you make GUI components for your java applications. This tutorial gives programs and examples to create Swing GUI.","keywords":"java","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/james","name":"James Hartman"},"dateModified":"2026-06-23T16:23:40+05:30","image":{"@id":"https://www.guru99.com/images/java-swing-tutorial.png"},"copyrightYear":"2026","name":"Java Swing Tutorial: How to Create a GUI Application in Java","subjectOf":[{"@type":"HowTo","name":"How to Make a GUI in Java with Example","description":"Now in this Java GUI Tutorial, let's understand how to create a GUI in Java with Swings in Java examples.","step":[{"@type":"HowToStep","name":"Step 1) Copy code into an editor","text":"In first step Copy the following code into an editor","url":"https://www.guru99.com/java-swing-gui.html#step1"},{"@type":"HowToStep","name":"Step 2) Run the code","text":"Next step, Save, Compile, and Run the code","url":"https://www.guru99.com/java-swing-gui.html#step2"},{"@type":"HowToStep","name":"Step 3) Copy following code into an editor","text":"Now let's Add a Button to our frame. Copy following code into an editor from given Java UI Example","url":"https://www.guru99.com/java-swing-gui.html#step3"},{"@type":"HowToStep","name":"Step 4) Execute the code","text":"Now Execute the code. You will get a big button","image":"https://www.guru99.com/images/uploads/2012/06/java-sswing-button.jpg","url":"https://www.guru99.com/java-swing-gui.html#step4"},{"@type":"HowToStep","name":"Step 5) Add two buttons","text":"How about adding two buttons? Copy the following code into an editor.","url":"https://www.guru99.com/java-swing-gui.html#step5"},{"@type":"HowToStep","name":"Step 6) Save &amp; Run the program","text":"Next, Save, Compile, and Run the program.","url":"https://www.guru99.com/java-swing-gui.html#step6"},{"@type":"HowToStep","name":"Step 7) Check output","text":"Unexpected output =? Buttons are getting overlapped.","url":"https://www.guru99.com/java-swing-gui.html#step7"},{"@type":"HowToStep","name":"Step 8) Create chat frame","text":"Next step, How about creating a chat frame like below?","image":"https://www.guru99.com/images/uploads/2012/06/java-swing-chat-frame.jpg","url":"https://www.guru99.com/java-swing-gui.html#step8"}]},{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"Difference between AWT, Swing, and JavaFX?","acceptedAnswer":{"@type":"Answer","text":"AWT uses native widgets. Swing is pure Java. JavaFX is modern."}},{"@type":"Question","name":"Can AI convert Swing apps to JavaFX?","acceptedAnswer":{"@type":"Answer","text":"Yes. Copilot translates components."}},{"@type":"Question","name":"How does AI help Swing developers?","acceptedAnswer":{"@type":"Answer","text":"AI generates JFrame code; flags EDT issues."}},{"@type":"Question","name":"Is Swing recommended for new desktop apps?","acceptedAnswer":{"@type":"Answer","text":"Oracle recommends JavaFX. Swing for legacy."}},{"@type":"Question","name":"Top-level container in Swing?","acceptedAnswer":{"@type":"Answer","text":"JFrame is the standard window."}},{"@type":"Question","name":"Most flexible Swing layout?","acceptedAnswer":{"@type":"Answer","text":"GridBagLayout is most flexible."}},{"@type":"Question","name":"Why run Swing code on the EDT?","acceptedAnswer":{"@type":"Answer","text":"Swing is not thread safe; use SwingUtilities.invokeLater."}},{"@type":"Question","name":"How to change Swing look and feel?","acceptedAnswer":{"@type":"Answer","text":"Use UIManager.setLookAndFeel."}}]}],"@id":"https://www.guru99.com/java-swing-gui.html#schema-44950","isPartOf":{"@id":"https://www.guru99.com/java-swing-gui.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/java-swing-gui.html#webpage"}}]}
```
