Back to the 2026 paper

Module 5: Introduction to AWT and Applets

20267m

Construct an AWT application containing a frame, label, text field and button. Develop an event-handling mechanism that displays a message when the button is pressed.

Worked SolutionAI Assisted

AWT event-handling example

import java.awt.*;
import java.awt.event.*;

public class GreetingApp {
    public static void main(String[] args) {
        Frame frame = new Frame("Greeting");
        Label label = new Label("Enter your name:");
        TextField input = new TextField(18);
        Button button = new Button("Greet");
        Label output = new Label(" ");
        Panel panel = new Panel();
        panel.add(label); panel.add(input); panel.add(button); panel.add(output);
        button.addActionListener(e -> output.setText("Hello, " + input.getText()));
        frame.add(panel);
        frame.setSize(420, 120);
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) { frame.dispose(); }
        });
        frame.setVisible(true);
    }
}

The button is the event source. addActionListener registers a listener; a click generates an ActionEvent and invokes the handler. The window listener closes the frame cleanly.

Similar questions

Free AppAndroid App

Coursify Android App

Generate AI courses, take quick quizzes between classes, and master complex engineering concepts on the go.