Event Handling in Java Using AWT_250311_094957
Event Handling in Java Using AWT_250311_094957
In Java, Abstract Window Toolkit (AWT) and Swing provide various components to create Graphical
User Interfaces (GUIs). These components include Frame, Panel, Button, Label, Checkbox, TextField,
and more.
• In Java, Component is the base class for all graphical user interface (GUI) elements that can
be displayed on the screen.
• All GUI elements such as Button, Label, TextField, Checkbox, Panel, List, Choice, and Canvas
are subclasses of Component.
• It provides methods to set size, color, font, visibility, and event handling.
• User Interaction: Can accept user inputs (e.g., Button click, text input).
• Event Handling: Listens to user actions like mouse clicks or keyboard presses.
// Create Components
frame.setLayout(new FlowLayout());
frame.add(label);
frame.add(nameField);
frame.add(sugarCheckbox);
frame.add(orderButton);
// Frame Properties
frame.setSize(300, 200);
frame.setVisible(true);
}
Container in Java (AWT & Swing)
What is a Container in Java?
• A Container in Java is a special type of Component that can hold other components inside it.
• Containers help in grouping components (like buttons, labels, text fields) to manage layout
efficiently.
Types of Containers
o Frame
o Window
o Dialog
o Panel
o ScrollPane
• It is a subclass of Container and does not have a title bar, menu bar, or border by default.
• Unlike Frame, a Window cannot exist independently; it requires another Frame or Window
as its owner.
✔ Top-level container (it does not need to be placed inside another container).
✔ No title bar or borders (unlike Frame).
✔ Used for splash screens, popup windows, and custom dialogs.
✔ Cannot be minimized or maximized (unless it’s a subclass like Frame or Dialog).
✔ Requires an existing Frame or Window as a parent.
• This pop-up does not have a title bar or minimize/maximize buttons—just a confirmation
dialog.
Conclusion
What is a Frame?
• A Frame is a top-level window in Java AWT that contains a title bar, border, and buttons
(minimize, maximize, close).
• When you open a banking app, the main screen appears with:
import java.awt.event.*;
// Create Frame
// Set Layout
// Create Components
countryChoice.add("India");
countryChoice.add("USA");
countryChoice.add("UK");
countryChoice.add("Australia");
submitButton.addActionListener(new ActionListener() {
String languages = (java.getState() ? "Java " : "") + (python.getState() ? "Python " : "") +
(cpp.getState() ? "C++" : "");
messageLabel.setText("Registration Successful!");
System.out.println("Registration Details:");
});
frame.add(nameLabel); frame.add(nameField);
frame.add(emailLabel); frame.add(emailField);
frame.add(countryLabel); frame.add(countryChoice);
frame.add(addressLabel); frame.add(addressArea);
frame.add(submitButton); frame.add(messageLabel);
frame.setSize(400, 400);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
frame.dispose();
});
}
Output
+-----------------------------------+
|-----------------------------------|
| Email: [ _______________ ] |
| Known Languages: |
| Country: [ India ▼ ] |
| Address: |
| [ ______________________ ] |
| [ ______________________ ] |
| [ ______________________ ] |
+-----------------------------------+