Open In App

Java AWT Tutorial

Last Updated : 27 Sep, 2025
Comments
Improve
Suggest changes
24 Likes
Like
Report

AWT (Abstract Window Toolkit) is a part of the Java Foundation Classes (JFC) used to create GUI (Graphical User Interface) or window-based applications. It provides platform-independent libraries but relies on native OS components for rendering, making it a heavyweight and platform-dependent application.

  • Part of java.awt package.
  • Provides GUI components like Button, Label, TextField, Checkbox, Choice, List, Canvas, etc.
  • Heavyweight components: Depend on the underlying OS for look and feel.
  • Platform-dependent appearance: AWT apps look like Windows apps on Windows, Mac apps on macOS, etc.

1. Types of Containers in Java AWT

There are four types of containers in Java AWT:

  • Window: Window is a top-level container that represents a graphical window or dialog box. The Window class extends the Container class, which means it can contain other components, such as buttons, labels and text fields.
  • Panel: Panel is a container class in Java. It is a lightweight container that can be used for grouping other components within a window or a frame.
  • Frame: The Frame is the container that contains the title bar and border and can have menu bars.
  • Dialog: A dialog box is a temporary window an application creates to retrieve user input.

2. Common AWT Components & Constructors

Learn the basics of the Abstract Window Toolkit (AWT) in Java, for both beginners and experienced developers.

3. Layout Managers in Java AWT

Layout managers (FlowLayout, BorderLayout, GridLayout, CardLayout) control component arrangement in containers.

4. Event Handling Components in Java AWT

Event handlers such as ActionListener, MouseListener, ItemListener, KeyListener and WindowListener are used to capture user actions and execute the corresponding response in GUI applications.

5. Java AWT Examples

1. Hello World in Java AWT

Hello, World is was the first step in learning Java. So, let us program our first Program in Java AWT as Hello World using Labels and Frames.

Below is the implementation of the above method:

Java
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

// Driver Class
public class AWT_Example {
    // main function
    public static void main(String[] args)
    {
        // Declaring a Frame and Label
        Frame frame = new Frame("Basic Program");
        Label label = new Label("Hello World!");

        // Aligning the label to CENTER
        label.setAlignment(Label.CENTER);

        // Adding Label and Setting the Size of the Frame
        frame.add(label);
        frame.setSize(300, 300);

        // Making the Frame visible
        frame.setVisible(true);

        // Using WindowListener for closing the window
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
    }
}

Running:

javac AWT_Example.java
java AWT_Example

Output:

Java AWT Hello World

2. Java AWT Program to create Button

Below is the implementation of the Java AWT Program to create a Button:

Java
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Button_Example {
    // main function
    public static void main(String[] args)
    {
        // Creating instance of frame with the label
        Frame frame = new Frame("Example 2");

        // Creating instance of button with label
        Button button = new Button("Click Here");

        // Setting the position for the button in frame
        button.setBounds(80, 100, 64, 30);

        // Adding button to the frame
        frame.add(button);

        // setting size, layout and visibility of frame
        frame.setSize(300, 300);
        frame.setLayout(null);
        frame.setVisible(true);

        // Using WindowListener for closing the window
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
    }
}

Run

javac Button_Example.java
java Button_Example

Output:

Java AWT Button Output


Article Tags :

Explore