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.

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.

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.

First, import the necessary Swing libraries:
```java import javax.swing.*; import java.awt.*; ```
Example 1: Simple GUI with Button and Label

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

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 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




















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!