Skip to main content

Overview

The Abstract Window Toolkit (AWT) is the foundational windowing toolkit for Java desktop applications. Located in the java.awt package, AWT provides the basic building blocks for creating graphical user interfaces, including:
  • Native platform components (windows, buttons, text fields)
  • Graphics rendering and 2D drawing capabilities
  • Event handling infrastructure
  • Layout management for component positioning
  • Color, font, and image support

Core Classes

Component Hierarchy

AWT follows a hierarchical component model:
java.lang.Object
  └── java.awt.Component
        ├── java.awt.Container
        │     ├── java.awt.Window
        │     │     ├── java.awt.Frame
        │     │     └── java.awt.Dialog
        │     └── java.awt.Panel
        ├── java.awt.Button
        ├── java.awt.Label
        ├── java.awt.TextField
        └── java.awt.Canvas

java.awt.Component

Base class for all AWT visual components. Provides fundamental capabilities for rendering, event handling, and component lifecycle.Key Methods:
paint(Graphics g)
void
Paints the component. Override to provide custom rendering.
setSize(int width, int height)
void
Sets the size of the component in pixels.
setVisible(boolean visible)
void
Shows or hides the component.
addComponentListener(ComponentListener l)
void
Registers a listener for component events (moved, resized, shown, hidden).
// Custom component example
class CustomComponent extends Component {
    @Override
    public void paint(Graphics g) {
        g.setColor(Color.BLUE);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(Color.WHITE);
        g.drawString("Custom Component", 10, 20);
    }
    
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 100);
    }
}

Graphics and Rendering

Graphics2D API

The Graphics2D class extends Graphics to provide sophisticated control over geometry, coordinate transformations, color management, and text layout. From source (java.awt.Graphics2D:42-46):
This Graphics2D class extends the Graphics class to provide more sophisticated control over geometry, coordinate transformations, color management, and text layout. This is the fundamental class for rendering 2-dimensional shapes, text and images on the Java platform.
public class DrawingPanel extends Panel {
    @Override
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        
        // Enable anti-aliasing
        g2d.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON
        );
        
        // Draw shapes
        g2d.setColor(Color.RED);
        g2d.fillRect(10, 10, 100, 50);
        
        g2d.setColor(Color.BLUE);
        g2d.drawOval(120, 10, 100, 100);
        
        // Draw text
        g2d.setColor(Color.BLACK);
        g2d.setFont(new Font("Arial", Font.BOLD, 16));
        g2d.drawString("Graphics2D Example", 10, 140);
    }
}

Event Handling

AWT uses a delegation event model where event listeners are registered with components.

Event Listeners

From source (java.awt.event.ActionListener:30-51):
The listener interface for receiving action events. The class that is interested in processing an action event implements this interface, and the object created with that class is registered with a component, using the component’s addActionListener method.
import java.awt.*;
import java.awt.event.*;

public class ActionExample {
    public static void main(String[] args) {
        Frame frame = new Frame("Action Listener Example");
        Button button = new Button("Click Me");
        
        // Add action listener
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Button clicked!");
            }
        });
        
        // Lambda syntax (Java 8+)
        button.addActionListener(e -> 
            System.out.println("Button clicked!")
        );
        
        frame.add(button);
        frame.setSize(300, 200);
        frame.setVisible(true);
    }
}

Layout Managers

Layout managers control the positioning and sizing of components within containers.

BorderLayout

From source (java.awt.BorderLayout:31-43):
A border layout lays out a container, arranging and resizing its components to fit in five regions: north, south, east, west, and center. Each region may contain no more than one component.
Frame frame = new Frame("BorderLayout Example");
frame.setLayout(new BorderLayout());

frame.add(new Button("North"), BorderLayout.NORTH);
frame.add(new Button("South"), BorderLayout.SOUTH);
frame.add(new Button("East"), BorderLayout.EAST);
frame.add(new Button("West"), BorderLayout.WEST);
frame.add(new TextArea("Center"), BorderLayout.CENTER);

frame.setSize(400, 300);
frame.setVisible(true);

FlowLayout

Panel panel = new Panel();
panel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));

panel.add(new Button("Button 1"));
panel.add(new Button("Button 2"));
panel.add(new Button("Button 3"));
panel.add(new Button("Button 4"));

GridLayout

Panel panel = new Panel();
panel.setLayout(new GridLayout(3, 2, 5, 5));  // 3 rows, 2 cols

for (int i = 1; i <= 6; i++) {
    panel.add(new Button("Button " + i));
}

GridBagLayout

Most flexible but complex layout manager:
Panel panel = new Panel();
GridBagLayout layout = new GridBagLayout();
panel.setLayout(layout);

GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;

gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 2;
panel.add(new Label("Name:"), gbc);

gbc.gridx = 2;
gbc.gridy = 0;
gbc.gridwidth = 3;
gbc.weightx = 1.0;
panel.add(new TextField(), gbc);

Complete Application Example

import java.awt.*;
import java.awt.event.*;

public class AWTCalculator extends Frame implements ActionListener {
    private TextField display;
    private String operator = "";
    private double num1 = 0, num2 = 0;
    
    public AWTCalculator() {
        setTitle("AWT Calculator");
        setLayout(new BorderLayout());
        
        // Display
        display = new TextField("0");
        display.setEditable(false);
        display.setFont(new Font("Arial", Font.BOLD, 20));
        add(display, BorderLayout.NORTH);
        
        // Button panel
        Panel buttonPanel = new Panel();
        buttonPanel.setLayout(new GridLayout(4, 4, 5, 5));
        
        String[] buttons = {
            "7", "8", "9", "/",
            "4", "5", "6", "*",
            "1", "2", "3", "-",
            "0", "C", "=", "+"
        };
        
        for (String label : buttons) {
            Button button = new Button(label);
            button.setFont(new Font("Arial", Font.PLAIN, 18));
            button.addActionListener(this);
            buttonPanel.add(button);
        }
        
        add(buttonPanel, BorderLayout.CENTER);
        
        // Window listener for close
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        
        setSize(300, 400);
        setVisible(true);
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        
        if (command.matches("[0-9]")) {
            String current = display.getText();
            if (current.equals("0")) {
                display.setText(command);
            } else {
                display.setText(current + command);
            }
        } else if (command.equals("C")) {
            display.setText("0");
            num1 = num2 = 0;
            operator = "";
        } else if (command.equals("=")) {
            num2 = Double.parseDouble(display.getText());
            double result = calculate(num1, num2, operator);
            display.setText(String.valueOf(result));
            operator = "";
        } else {
            num1 = Double.parseDouble(display.getText());
            operator = command;
            display.setText("0");
        }
    }
    
    private double calculate(double n1, double n2, String op) {
        switch (op) {
            case "+": return n1 + n2;
            case "-": return n1 - n2;
            case "*": return n1 * n2;
            case "/": return n2 != 0 ? n1 / n2 : 0;
            default: return n2;
        }
    }
    
    public static void main(String[] args) {
        new AWTCalculator();
    }
}

See Also

Build docs developers (and LLMs) love