Java Swing Tutorial — How to Create a GUI in Java with Examples
Introduction to Java Swing
Java Swing is a powerful GUI (Graphical User Interface) library provided by Java to create interactive and user-friendly applications. It offers a set of components and containers that allow developers to build window-based applications with ease.
Setting up the Java Development Environment
Before diving into Java Swing, ensure you have the following installed on your system:
- Java Development Kit (JDK)
- Integrated Development Environment (IDE) such as Eclipse or IntelliJ IDEA (optional but recommended for ease of development)
Creating a Simple Swing Application
Let's start by creating a basic Swing application with a simple window.
Step 1: Import Required Packages
In your Java file, import the necessary Swing packages:
javaimport javax.swing.*;
Step 2: Create the Main Window
Create the main class that extends JFrame to create a window:
javapublic class SimpleSwingApp extends JFrame {
    // Constructor
    public SimpleSwingApp() {
        // Set the window title
        setTitle("Simple Swing Application");
        // Set the window size (width, height) in pixels
        setSize(400, 300);
        // Terminate the application when the window is closed
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    
    // Entry point of the application
    public static void main(String[] args) {
        // Ensure GUI components are created on the Event Dispatch Thread (EDT)
        SwingUtilities.invokeLater(() -> {
            SimpleSwingApp app = new SimpleSwingApp();
            // Make the window visible
            app.setVisible(true);
        });
    }
}
Step 3: Run the Application
Compile and run the application. You should see a simple window titled "Simple Swing Application."
Adding Components to the Swing Window
Now, let's add some components to our window, such as buttons and labels.
Step 1: Import Required Packages
Ensure you have the necessary package imported:
javaimport javax.swing.*;
import java.awt.*;
Step 2: Create the Main Window and Add Components
javapublic class SwingAppWithComponents extends JFrame {
    // Constructor
    public SwingAppWithComponents() {
        setTitle("Swing Application with Components");
        setSize(400, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        
        // Create a panel to hold components
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());
        
        // Create a label
        JLabel label = new JLabel("Welcome to Java Swing!");
        
        // Create a button
        JButton button = new JButton("Click Me!");
        
        // Add the label and button to the panel
        panel.add(label);
        panel.add(button);
        
        // Add the panel to the main window
        add(panel);
    }
    
    // Entry point of the application
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            SwingAppWithComponents app = new SwingAppWithComponents();
            app.setVisible(true);
        });
    }
}
Step 3: Run the Application
Compile and run the application. You should see a window with a label and a button.
Conclusion
Java Swing provides a robust toolkit for creating graphical user interfaces in Java. With this tutorial, you have learned the basics of setting up a Swing application, creating a simple window, and adding components to it. You can now explore and leverage the various Swing components to build more sophisticated and interactive GUI applications in Java.
 
 
 
0 Comments