⬅ Previous Next ➡

Java GUI (Swing & AWT)

Java GUI (AWT & Swing): Components, Layout Managers, Event Handling & Basic Apps
  • GUI (Graphical User Interface) allows users to interact using windows, buttons, text fields, etc.
  • AWT (Abstract Window Toolkit) provides basic GUI components (heavyweight).
  • Swing is built on top of AWT and provides richer components (lightweight) like JFrame, JButton, JTextField.
  • GUI uses Layout Managers to arrange components and Event Handling to respond to user actions.

1) AWT Components (Basic)

  • Common AWT classes: Frame, Label, Button, TextField, TextArea, Checkbox.
  • AWT components are heavyweight (depend on OS).
import java.awt.Frame;
import java.awt.Label;

class AwtFrameDemo {
    public static void main(String[] args) {
        Frame f = new Frame("AWT Window");
        Label l = new Label("Hello AWT");

        f.add(l);
        f.setSize(300, 200);
        f.setVisible(true);
    }
}

2) Swing Components (Basic)

  • Common Swing classes: JFrame, JLabel, JButton, JTextField, JTextArea, JCheckBox.
  • Swing components are lightweight and more customizable.
import javax.swing.JFrame;
import javax.swing.JLabel;

class SwingFrameDemo {
    public static void main(String[] args) {
        JFrame f = new JFrame("Swing Window");
        JLabel l = new JLabel("Hello Swing");

        f.add(l);
        f.setSize(320, 220);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

3) Layout Managers

  • Layout Manager controls how components are placed inside a container.
  • Common layouts:
  • FlowLayout: left-to-right flow (default for Panel).
  • BorderLayout: North, South, East, West, Center (default for Frame).
  • GridLayout: rows and columns grid.
  • BoxLayout: vertical/horizontal arrangement (Swing).
import javax.swing.*;
import java.awt.*;

class LayoutDemo {
    public static void main(String[] args) {
        JFrame f = new JFrame("Layout Demo");
        f.setLayout(new GridLayout(2, 2, 10, 10));

        f.add(new JButton("One"));
        f.add(new JButton("Two"));
        f.add(new JButton("Three"));
        f.add(new JButton("Four"));

        f.setSize(350, 200);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

4) Event Handling (ActionListener)

  • Event = user action (click, key press, mouse move).
  • Listener handles events (ActionListener for button click).
  • Common listeners: ActionListener, MouseListener, KeyListener, WindowListener.
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class EventDemo {
    public static void main(String[] args) {

        JFrame f = new JFrame("Event Demo");
        JButton btn = new JButton("Click");
        JLabel lbl = new JLabel("Status: Waiting...");

        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                lbl.setText("Status: Button Clicked!");
            }
        });

        f.setLayout(null);
        btn.setBounds(30, 30, 120, 35);
        lbl.setBounds(30, 80, 200, 30);

        f.add(btn);
        f.add(lbl);

        f.setSize(320, 200);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

5) Basic GUI Application (Mini Calculator - Swing)

  • Example: add two numbers using text fields and button click event.
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class MiniCalculator {
    public static void main(String[] args) {

        JFrame f = new JFrame("Mini Calculator");

        JLabel l1 = new JLabel("Num1:");
        JLabel l2 = new JLabel("Num2:");
        JLabel out = new JLabel("Result: ");

        JTextField t1 = new JTextField();
        JTextField t2 = new JTextField();

        JButton addBtn = new JButton("Add");

        addBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    int a = Integer.parseInt(t1.getText().trim());
                    int b = Integer.parseInt(t2.getText().trim());
                    out.setText("Result: " + (a + b));
                } catch (Exception ex) {
                    out.setText("Result: Invalid Input!");
                }
            }
        });

        f.setLayout(null);

        l1.setBounds(30, 20, 60, 25);
        t1.setBounds(100, 20, 150, 25);

        l2.setBounds(30, 60, 60, 25);
        t2.setBounds(100, 60, 150, 25);

        addBtn.setBounds(100, 100, 80, 30);
        out.setBounds(30, 140, 250, 25);

        f.add(l1); f.add(t1);
        f.add(l2); f.add(t2);
        f.add(addBtn);
        f.add(out);

        f.setSize(320, 230);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }
}

6) Quick Notes

  • Swing is preferred over AWT for modern Java desktop apps.
  • Use layout managers to avoid manual positioning (null layout is not recommended for big apps).
  • Event handling connects user actions to code logic.
⬅ Previous Next ➡