Skip to main content

Java AWT

Introduction to Java AWT

Java Abstract Window Toolkit (AWT) is a platform-independent windowing, graphics, and user-interface toolkit for the Java Programming Language. It's part of Java Foundation Classes (JFC) — an API for providing a graphical user interface for Java programs. AWT includes a rich set of controls or components, layout managers, events, and more.

Understanding AWT Components

AWT includes a large number of classes and interfaces, but the most commonly used are the following:

  • Component: This is the abstract base class for all AWT components. It represents the graphical objects.

  • Container: A component that can contain other AWT components.

  • Panel: A concrete subclass of container. It provides space in which an application can attach any other components.

  • Window: A top-level container that has no borders or menubar. This is used to create a child window.

  • Frame: This is a top-level window with a title and a border.

  • Canvas: A blank rectangular area where the application can draw or trap input events from the user.

  • Button, Label, TextField, TextArea, Checkbox, Choice, List, Menu: These are various controls or components that inherit from the Component class.

Layout Managers

Layout managers are used to arrange components in a particular manner. Layout Manager is an interface that is implemented by all the classes of layout managers. There are following classes that represent the layout managers:

  • FlowLayout: The FlowLayout is used to arrange the components in a line, one after another (in a flow). It is the default layout of applet or panel.

  • BorderLayout: The BorderLayout is used to arrange the components in five regions: north, south, east, west, and center.

  • GridLayout: The GridLayout is used to arrange the components in a rectangular grid.

  • CardLayout: The CardLayout object treats each component in the container as a card. Only one card is visible at a time, and the container acts as a stack of cards.

  • GridBagLayout: This is the most flexible layout manager class.

Implementing Event Handling

Java AWT (Abstract Window Toolkit) is an API to develop GUI or window-based applications in java. AWT event handling introduces several classes and methods that are used to handle events in Java. Java AWT components can generate events on occurring an action by the user. An event can be the pressing of a button, the entering of a character via the keyboard, the clicking of a mouse, etc.

Creating an Application using AWT

Let's create a simple login form using Java AWT that includes a label, text field, and a button.

import java.awt.*;  
class First extends Frame{
First(){
Button b = new Button("click me");
b.setBounds(30,100,80,30);
add(b);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public static void main(String args[]){
First f = new First();
}
}

In this example, we are creating a button and adding it on the frame. The setBounds(int xaxis, int yaxis, int width, int height) method is used in the above example that sets the position of the awt button.

Java AWT is powerful and provides a robust platform for creating GUI applications in Java. With a clear understanding of components, layout managers, and event handling, you can create complex and user-friendly GUI applications.