The Abstract Window Toolkit (AWT) is a set of APIs provided by Java for building graphical user interfaces (GUIs). It is part of the Java Foundation Classes (JFC) and provides a rich set of components for creating window-based applications. AWT is the original GUI toolkit in Java, and it is built on top of the native system libraries, which means that AWT components rely on the underlying operating system’s windowing system.
Platform Independence: AWT provides a consistent API across different platforms, allowing developers to write applications that can run on various operating systems without modification.
Lightweight Components: AWT components are heavyweight, meaning they are implemented using the native GUI components of the host operating system. This gives AWT applications a look and feel that is native to the operating system.
Event Handling: AWT uses an event-driven programming model, allowing the application to respond to user actions like mouse clicks and keyboard input.
Layout Management: AWT provides various layout managers to arrange components within a container, making it easier to create user-friendly interfaces.
Graphics and Image Handling: AWT provides classes for drawing shapes, images, and text, enabling developers to create custom graphics.
AWT includes several components, such as buttons, text fields, labels, lists, checkboxes, and more. Here are some of the most commonly used AWT components:
Here’s a simple example demonstrating some basic AWT components:
import java.awt.*;
import java.awt.event.*;
public class AWTExample {
public static void main(String[] args) {
Frame frame = new Frame("AWT Example");
frame.setSize(400, 300);
frame.setLayout(new FlowLayout());
Label label = new Label("Enter your name:");
TextField textField = new TextField(20);
Button button = new Button("Submit");
// Action Listener for button
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String name = textField.getText();
System.out.println("Hello, " + name);
}
});
frame.add(label);
frame.add(textField);
frame.add(button);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
frame.setVisible(true);
}
}
AWT provides several layout managers to arrange components in a container. The most common layout managers are:
Here’s an example illustrating the use of different layout managers:
import java.awt.*;
import java.awt.event.*;
public class LayoutExample {
public static void main(String[] args) {
Frame frame = new Frame("Layout Manager Example");
frame.setSize(400, 300);
frame.setLayout(new BorderLayout());
Button northButton = new Button("North");
Button southButton = new Button("South");
Button eastButton = new Button("East");
Button westButton = new Button("West");
Button centerButton = new Button("Center");
frame.add(northButton, BorderLayout.NORTH);
frame.add(southButton, BorderLayout.SOUTH);
frame.add(eastButton, BorderLayout.EAST);
frame.add(westButton, BorderLayout.WEST);
frame.add(centerButton, BorderLayout.CENTER);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
frame.setVisible(true);
}
}
In this example, the BorderLayout
manager arranges buttons in five regions, demonstrating how different layout managers can control component placement.
AWT employs an event-driven model where components generate events in response to user interactions. These events are handled using event listeners.
Here’s a simple example showing how to handle button click events:
import java.awt.*;
import java.awt.event.*;
public class EventHandlingExample {
public static void main(String[] args) {
Frame frame = new Frame("Event Handling Example");
frame.setSize(400, 300);
frame.setLayout(new FlowLayout());
Button button = new Button("Click Me");
Label label = new Label("");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
label.setText("Button Clicked!");
}
});
frame.add(button);
frame.add(label);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
frame.setVisible(true);
}
}
In this example, when the button is clicked, the label’s text is updated to indicate that the button was clicked. This demonstrates the use of ActionListener
to handle button click events.
AWT provides a Graphics
class that can be used to draw shapes, text, and images. You can override the paint
method of a component to perform custom painting.
Here’s an example showing how to draw shapes using AWT:
import java.awt.*;
import javax.swing.*;
public class DrawingExample extends Canvas {
public void paint(Graphics g) {
g.setColor(Color.RED);
g.fillRect(50, 50, 100, 100); // Draw a red rectangle
g.setColor(Color.BLUE);
g.fillOval(200, 50, 100, 100); // Draw a blue oval
g.setColor(Color.GREEN);
g.drawString("Hello, AWT!", 100, 200); // Draw a string
}
public static void main(String[] args) {
Frame frame = new Frame("Drawing Example");
DrawingExample drawingExample = new DrawingExample();
frame.add(drawingExample);
frame.setSize(400, 300);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
}
In this example, a Canvas
is used to perform custom painting. The paint
method is overridden to draw a rectangle, an oval, and a string on the canvas.