Java Swing, a powerful GUI toolkit, enables developers to create desktop applications with a rich, interactive user interface. If you're new to Java Swing, understanding the basics and seeing practical examples can significantly enhance your learning experience. Let's delve into some Java Swing program examples, complete with their outputs.

Java Swing UI Redone
Java Swing UI Redone

Before we dive into the examples, ensure you have Java Development Kit (JDK) installed, and you're familiar with the basic syntax of Java. Now, let's explore two main topics: creating simple GUI components and handling events in Java Swing.

Basic GUI in Java | Swing GUI Tutorial for Beginners
Basic GUI in Java | Swing GUI Tutorial for Beginners

Creating Simple GUI Components

Java Swing offers a wide range of components like buttons, labels, text fields, and more. Let's create a simple Java Swing program with a button and a label.

the press button is shown in this screenshote screen shot, and it appears to be empty
the press button is shown in this screenshote screen shot, and it appears to be empty

First, import the necessary Swing libraries:

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

Example 1: Simple GUI with Button and Label

the student registration screen is shown in yellow
the student registration screen is shown in yellow

Here's a simple Java Swing program with a button and a label:

```java public class SimpleGUI extends JFrame { private JLabel label; public SimpleGUI() { setLayout(new FlowLayout()); label = new JLabel("Hello, World!"); add(label); JButton button = new JButton("Change Text"); button.addActionListener(e -> label.setText("Text changed!")); add(button); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(200, 100); setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(() -> new SimpleGUI()); } } ```

When you run this program, you'll see a window with a "Hello, World!" label and a "Change Text" button. Clicking the button changes the label's text to "Text changed!".

Example 2: GUI with Text Field and Label

Java Swing Tutorial
Java Swing Tutorial

In this example, we'll create a GUI with a text field and a label. The label will display the text entered into the text field:

```java import javax.swing.*; import java.awt.*; public class TextFieldGUI extends JFrame { private JTextField textField; private JLabel label; public TextFieldGUI() { setLayout(new FlowLayout()); textField = new JTextField(15); add(textField); label = new JLabel(""); add(label); textField.addActionListener(e -> label.setText("You entered: " + textField.getText())); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(250, 100); setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(() -> new TextFieldGUI()); } } ```

In this example, when you type text into the text field and press Enter, the label displays the entered text.

Handling Events in Java Swing

Java Swings #1  Simple Login UI
Java Swings #1 Simple Login UI

Java Swing uses the Observer design pattern to handle events. Let's explore two event handling examples: mouse click and key press events.

Example 3: Mouse Click Event

How to design a simple dashboard UI using Swing and Java -Netbeans
How to design a simple dashboard UI using Swing and Java -Netbeans
Java Cheatsheet for Beginners | Learn Java Basics in One Page
Java Cheatsheet for Beginners | Learn Java Basics in One Page
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
Java Bootcamp London & UK – Part Time Java Developer Program
Java Bootcamp London & UK – Part Time Java Developer Program
Java Swing : School Management System Inspiration
Java Swing : School Management System Inspiration
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
Java Data Types Made Easy for Beginners
Java Data Types Made Easy for Beginners
Java String Methods
Java String Methods
the screenshote screen shows how to use jump statement
the screenshote screen shows how to use jump statement
Java Cheat Sheet: Operators, Scanner & User Input ☕
Java Cheat Sheet: Operators, Scanner & User Input ☕
an image of a computer screen with the text'string a, now it is time to call '
an image of a computer screen with the text'string a, now it is time to call '
Create Swing GUI Project with Design Process in Java
Create Swing GUI Project with Design Process in Java
Logical Operators in Java: Think Smarter, Code Better ☕🚀
Logical Operators in Java: Think Smarter, Code Better ☕🚀
#1 JavaFX UI Design Dashboard Utility
#1 JavaFX UI Design Dashboard Utility
Structure of a Java Program + Hello World Example | Java Programming Basics 💻
Structure of a Java Program + Hello World Example | Java Programming Basics 💻
Java Collection Framework: ArrayList Explained Visually
Java Collection Framework: ArrayList Explained Visually
Top 5 Java Projects for Beginners
Top 5 Java Projects for Beginners
Encapsulation in Java Explained Simply | OOP Concept with Example 💻✨
Encapsulation in Java Explained Simply | OOP Concept with Example 💻✨
Class and Object in Java Explained | OOP Concepts for Beginners
Class and Object in Java Explained | OOP Concepts for Beginners
the top 100 programming projects list is shown in black and white, with orange lettering
the top 100 programming projects list is shown in black and white, with orange lettering

In this example, we'll create a GUI with a button. Clicking the button will display a message box with the number of times the button has been clicked:

```java import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; public class MouseClickGUI extends JFrame { private int clickCount = 0; public MouseClickGUI() { setLayout(new FlowLayout()); JButton button = new JButton("Click me!"); button.addActionListener(e -> { clickCount++; JOptionPane.showMessageDialog(this, "Button clicked " + clickCount + " times."); }); add(button); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(200, 100); setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(() -> new MouseClickGUI()); } } ```

Each time you click the "Click me!" button, a message box displays the number of times the button has been clicked.

Example 4: Key Press Event

In this final example, we'll create a GUI with a text field. Pressing the Enter key in the text field will display the entered text in a message box:

```java import javax.swing.*; import java.awt.*; import java.awt.event.KeyEvent; public class KeyPressGUI extends JFrame { private JTextField textField; public KeyPressGUI() { setLayout(new FlowLayout()); textField = new JTextField(15); add(textField); textField.addKeyListener(e -> { if (e.getKeyCode() == KeyEvent.VK_ENTER) { JOptionPane.showMessageDialog(this, "You entered: " + textField.getText()); } }); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(250, 100); setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(() -> new KeyPressGUI()); } } ```

When you type text into the text field and press Enter, a message box displays the entered text.

Java Swing offers a wealth of possibilities for creating engaging and interactive user interfaces. By understanding the basics and exploring these examples, you'll be well on your way to creating your own Java Swing applications. Happy coding!