1.
Components and Containers of AWT
Components are individual elements of a graphical user interface (GUI) like buttons, text
fields, and labels. Containers are used to hold and organize components, such as
frames, panels, and applets.
Example:
java
Copy code
import java.awt.*;
import java.awt.event.*;
public class AWTExample {
public static void main(String[] args) {
Frame frame = new Frame("AWT Example");
Button button = new Button("Click Me");
frame.add(button); // Adding button to the frame
frame.setSize(300, 200);
frame.setLayout(new FlowLayout());
frame.setVisible(true);
}
}
2. Delegation Event Model
The Delegation Event Model involves event sources (like buttons) generating events,
which are handled by event listeners that implement specific event-handling methods.
Sketch:
lua
Copy code
+------------+ +-------------+ +--------------+
| Event | -----> | Event | -----> | Event |
| Source | | Listener | | Handler |
+------------+ +-------------+ +--------------+
Example Program:
java
Copy code
import java.awt.*;
import java.awt.event.*;
public class EventDelegationExample {
public static void main(String[] args) {
Frame frame = new Frame("Event Delegation Example");
Button button = new Button("Click Me");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked");
}
});
frame.add(button);
frame.setSize(300, 200);
frame.setLayout(new FlowLayout());
frame.setVisible(true);
}
}
3. Two AWT Controls with Examples
Button:
java
Copy code
Button button = new Button("Click Me");
frame.add(button);
TextField:
java
Copy code
TextField textField = new TextField(20);
frame.add(textField);
4. Java Program Using Listeners for Handling Keyboard Events
java
Copy code
import java.awt.*;
import java.awt.event.*;
public class KeyEventExample {
public static void main(String[] args) {
Frame frame = new Frame("Key Event Example");
TextField textField = new TextField(20);
textField.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
System.out.println("Key Pressed: " + e.getKeyChar());
}
});
frame.add(textField);
frame.setSize(300, 200);
frame.setLayout(new FlowLayout());
frame.setVisible(true);
}
}
5. Program to Handle Mouse Events and Mouse Motion Events
java
Copy code
import java.awt.*;
import java.awt.event.*;
public class MouseEventExample {
public static void main(String[] args) {
Frame frame = new Frame("Mouse Event Example");
frame.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse Clicked at: " + e.getX() + ", " +
e.getY());
}
});
frame.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseMoved(MouseEvent e) {
System.out.println("Mouse Moved to: " + e.getX() + ", " +
e.getY());
}
});
frame.setSize(300, 200);
frame.setLayout(new FlowLayout());
frame.setVisible(true);
}
}
6. ActionEvent and AdjustmentEvent Classes
ActionEvent: Generated when a button is clicked, a menu item is selected, etc.
java
Copy code
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Action performed");
}
});
AdjustmentEvent: Generated by adjustable objects like scrollbars.
java
Copy code
scrollbar.addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
System.out.println("Adjustment made");
}
});
7. ActionEvent with Example
Example:
java
Copy code
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked");
}
});
8. Lists and Panels, Program for List and ScrollPane
Lists: Used to display a list of items. Panels: Containers for organizing components.
Example Program:
java
Copy code
import java.awt.*;
import java.awt.event.*;
public class ListExample {
public static void main(String[] args) {
Frame frame = new Frame("List Example");
List list = new List(4, false); // Single-selection list
list.add("Item 1");
list.add("Item 2");
list.add("Item 3");
ScrollPane scrollPane = new ScrollPane();
scrollPane.add(list);
frame.add(scrollPane);
frame.setSize(300, 200);
frame.setLayout(new FlowLayout());
frame.setVisible(true);
}
}
9. Program to Handle Keyboard Events
(Refer to answer for Question 4)
10. Layout Managers Explained
GridLayout: Arranges components in a grid of rows and columns.
java
Copy code
frame.setLayout(new GridLayout(2, 2));
FlowLayout: Arranges components in a left-to-right flow.
java
Copy code
frame.setLayout(new FlowLayout());
CardLayout: Stacks components like cards, only one visible at a time.
java
Copy code
CardLayout cardLayout = new CardLayout();
frame.setLayout(cardLayout);
BorderLayout: Arranges components in five regions: north, south, east, west, and center.
java
Copy code
frame.setLayout(new BorderLayout());
frame.add(button, BorderLayout.NORTH);
GridBagLayout: A flexible grid-based layout with constraints for each component.
java
Copy code
GridBagLayout layout = new GridBagLayout();
frame.setLayout(layout);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
frame.add(button, gbc);