1.Introduction
Java Absract Window Toolkit(AWT) is a Graphical User Interface(GUI) library.It is used to create windows,buttons,textfields,labels,menus and other components for desktop applications.AWT provides a set of classes that allow programmers to design applications that users can interact with through a graphical interface instead of typing commands.It was one of the earliest GUI toolkits in Java and is found in the java.awt package.
2.Features of Java AWT
Java AWT has several important features:
- Platform Independance-Java programs that use AWT can run on any OS.
- GUI components-Provides textfields,labels,buttons,menus and may more components.
- Event handling-Allows programs to respond to use actions ie when user clicks a button a response is given to the action.
- Layout management-Helps organise components inside a window(there are different types of layout managers).
3.AWT Components
Components are the different visual elements that appear on the screen and they include:
- Button-Perfoms an action when clicked.
- Label-Displays text.
- TextField-Allows user to enter text.
- Checkbox-Allows selection of options.
- Frame-Main window container.
Example Code:
JButton btnLogin=new JButton();//creates empty button
btnLogin.setText("Login");//inside the button there should be a login text
4.Containers in AWT
These are components that hold other components.They help in structuring the interface and controling how components appear within a window
Examples include:
1. Frame
A Frame is the main top-level window of an application. It contains the title bar, borders, and control buttons such as minimize, maximize and close.
2. Panel
A Panel is a simple container used to group related components together within another container. Panels are often placed inside a Frame to organize elements in sections, making the interface easier to design and manage.
3. Dialog
A Dialog is a pop-up window used to display messages or request input from the user. Dialog boxes are commonly used for alerts, confirmations or data entry.
5.Layout Managers
They are used to organise components in a container.
Common layout managers include:
- BorderLayout-Divides a screen into 5 regions.
- GridLayout-Arranges components in a screen into rows
and columns.
- FlowLayout-Places components in a row from left to right.
- CardLayout-Allows switching between panel
s.
6.Event Handling in AWT
Event handling allows the program to respond to user actions.
Examples of events:
- Clicking a button.
- Typing in a text field.
- Selecting a checkbox.
Example:
- ActionListener
- MouseListener
- KeyListener
7.Simple AWT program
import java.awt.*;
public class SimpleAWT {
public static void main(String[] args) {
Frame frame = new Frame("My First AWT Program");
Button button = new Button("Click Me");
frame.add(button);
frame.setBackground(Color.CYAN);
frame.setSize(300,200);
frame.setLayout(new FlowLayout());
frame.setVisible(true);
}
}
NB:Ensure that SimpleAWT is also the filename incase you are using netbeans
A picture of the expected output:
This program creates:
- A window.
- A button inside the window.
- A background that is Cyan in color.

Top comments (0)