📖 AWT (Abstract Window Toolkit):
AWT is Java's original GUI framework in java.awt package. It provides platform-dependent GUI components — each AWT component maps to a native OS widget (uses OS rendering). AWT is the foundation on which Swing is built.
AWT Class Hierarchy:
java.lang.Object
└── java.awt.Component (abstract base — all GUI elements)
├── Button, Label, TextField, TextArea
├── Checkbox, Choice, List, Scrollbar, Canvas
└── java.awt.Container (can hold other components)
├── java.awt.Panel (no title bar — groups components)
└── java.awt.Window (top-level, no title bar)
├── java.awt.Frame ← MAIN WINDOW (title, border, buttons) ✓
└── java.awt.Dialog ← Popup window
AWT Class Hierarchy — Know this for exams!
| Class | Description | Key Methods |
| Component | Abstract base of all GUI widgets. Cannot be instantiated. | setSize(), setVisible(), setBackground(), setFont(), setEnabled() |
| Container | Can hold other components using layout manager | add(), remove(), setLayout(), getComponents() |
| Panel | Simple container. No title bar. Used to group components. | Inherits Container methods. Default layout: FlowLayout |
| Window | Top-level window with no decorations (no title/border) | setLocation(), toFront(), dispose() |
| Frame | Main application window with title bar, border, min/max/close buttons | setTitle(), setSize(), setVisible(), setResizable() |
| Canvas | Blank rectangular area for custom drawing | Override paint(Graphics g) to draw |
All AWT Components with Code:
import java.awt.*;
import java.awt.event.*;
public class AllComponentsDemo extends Frame implements ActionListener {
// Declare all components
Label lblName, lblCity, lblResult;
TextField tfName;
TextArea taAddress;
Checkbox cbJava, cbPython;
CheckboxGroup genderGroup;
Checkbox rbMale, rbFemale;
Choice cityChoice;
List skillList;
Scrollbar slider;
Button btnSubmit;
public AllComponentsDemo() {
setLayout(null); // Manual (absolute) positioning
// 1. LABEL — displays non-editable text
lblName = new Label("Name:");
lblName.setBounds(20, 30, 80, 25);
add(lblName);
// 2. TEXTFIELD — single-line text input
tfName = new TextField("Enter name", 20);
tfName.setBounds(110, 30, 200, 25);
add(tfName);
// 3. TEXTAREA — multi-line text input
lblCity = new Label("Address:");
lblCity.setBounds(20, 70, 80, 25);
taAddress = new TextArea("City, State", 4, 25); // 4 rows, 25 cols
taAddress.setBounds(110, 70, 200, 70);
add(lblCity); add(taAddress);
// 4. CHECKBOX — toggle (can select multiple)
cbJava = new Checkbox("Java", true); // pre-checked
cbPython = new Checkbox("Python", false);
cbJava.setBounds(20, 155, 100, 25);
cbPython.setBounds(130, 155, 100, 25);
add(cbJava); add(cbPython);
// 5. RADIO BUTTONS — CheckboxGroup makes them mutually exclusive
genderGroup = new CheckboxGroup();
rbMale = new Checkbox("Male", genderGroup, true);
rbFemale = new Checkbox("Female", genderGroup, false);
rbMale.setBounds(20, 190, 80, 25);
rbFemale.setBounds(110, 190, 90, 25);
add(rbMale); add(rbFemale);
// 6. CHOICE — dropdown (select ONE item)
cityChoice = new Choice();
cityChoice.add("Delhi");
cityChoice.add("Mumbai");
cityChoice.add("Ludhiana");
cityChoice.add("Bangalore");
cityChoice.select("Ludhiana"); // default selection
cityChoice.setBounds(20, 225, 180, 25);
add(cityChoice);
// 7. LIST — visible list (can allow multi-select)
skillList = new List(3, true); // 3 rows, multi-select = true
skillList.add("HTML"); skillList.add("CSS");
skillList.add("Java"); skillList.add("SQL");
skillList.setBounds(210, 225, 100, 65);
add(skillList);
// 8. SCROLLBAR — sliding value control
slider = new Scrollbar(Scrollbar.HORIZONTAL, 50, 10, 0, 110);
// (orientation, initialValue, visibleAmount, min, max)
slider.setBounds(20, 300, 290, 20);
add(slider);
// 9. BUTTON — clickable
btnSubmit = new Button("Submit");
btnSubmit.addActionListener(this);
btnSubmit.setBounds(110, 335, 100, 30);
add(btnSubmit);
// 10. RESULT LABEL
lblResult = new Label("");
lblResult.setBounds(20, 375, 310, 25);
add(lblResult);
setTitle("All AWT Components");
setSize(360, 420);
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){dispose();}
});
}
public void actionPerformed(ActionEvent e) {
String name = tfName.getText();
String gender = genderGroup.getSelectedCheckbox().getLabel();
String city = cityChoice.getSelectedItem();
String java = cbJava.getState() ? "Java" : "";
lblResult.setText(name + " | " + gender + " | " + city + " | " + java);
}
public static void main(String[] a) { new AllComponentsDemo(); }
}
// Output: Fill form → click Submit → result label shows: "Ankush | Male | Ludhiana | Java"
Canvas — Custom Drawing Area:
import java.awt.*;
import java.awt.event.*;
class DrawCanvas extends Canvas {
public void paint(Graphics g) {
// Draw various shapes
g.setColor(Color.RED); g.fillRect(20, 20, 100, 60);
g.setColor(Color.BLUE); g.fillOval(140, 20, 80, 80);
g.setColor(Color.GREEN); g.drawRoundRect(240, 20, 90, 60, 20, 20);
g.setColor(Color.ORANGE); g.fillArc(20, 110, 100, 80, 0, 270);
g.setColor(Color.BLACK); g.drawLine(0, 200, 350, 200);
g.setFont(new Font("Arial", Font.BOLD, 14));
g.setColor(Color.MAGENTA);
g.drawString("Java AWT Canvas Drawing", 30, 230);
}
}
public class CanvasDemo extends Frame {
public CanvasDemo() {
DrawCanvas c = new DrawCanvas();
c.setSize(360, 250);
c.setBackground(Color.WHITE);
add(c);
setTitle("Canvas Demo"); setSize(380, 280); setVisible(true);
}
public static void main(String[] a) { new CanvasDemo(); }
}