Java Notes Important Topics
Java Notes Important Topics
import java.awt.*;
import java.awt.event.*;
// Create button
Button button = new Button("Click Me");
Explanation of Code:
• A Frame is created as the main window.
• A Button is added with the label "Click Me".
• A Label is used to display messages.
• An ActionListener is attached to detect button clicks and update the label text.
• A WindowListener is used to close the window properly.
V.Basic Concepts of CheckBox in Java AWT
A CheckBox in Java AWT is a toggleable UI component that allows users to select or deselect
an option. It is represented by the Checkbox class in the java.awt package.
Key Features:
1. Constructor:
o Checkbox(String label): Creates a checkbox with the given label.
o Checkbox(String label, boolean state): Creates a checkbox with a predefined
state (checked or unchecked).
o Checkbox(String label, CheckboxGroup group, boolean state): Used for radio
button behavior.
2. Event Handling:
o ItemListener is used to detect checkbox state changes.
o The method itemStateChanged(ItemEvent e) is implemented to define the
action.
// Create checkboxes
Checkbox cb1 = new Checkbox("Option 1");
Checkbox cb2 = new Checkbox("Option 2");
cb1.addItemListener(itemListener);
cb2.addItemListener(itemListener);