0% found this document useful (0 votes)
2 views

Java unit 4

The document provides an overview of event handling in Java, explaining key concepts such as events, event sources, event classes, event listeners, and the delegation event model. It also covers how to handle mouse and keyboard events, the use of adapter classes, and various AWT components and layout managers. Examples are provided for creating GUI elements and managing their layout in Java applications.

Uploaded by

ariardy1396
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java unit 4

The document provides an overview of event handling in Java, explaining key concepts such as events, event sources, event classes, event listeners, and the delegation event model. It also covers how to handle mouse and keyboard events, the use of adapter classes, and various AWT components and layout managers. Examples are provided for creating GUI elements and managing their layout in Java applications.

Uploaded by

ariardy1396
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

EVENT HANDLING IN JAVA

1. What are Events in Java?


Answer: Events in Java are actions or occurrences, such as mouse clicks, key presses, or window actions,
that an application can respond to.

2. What are Event Sources in Java?


Answer: Event sources are objects that can generate events. Examples include GUI components like
buttons, text fields, and windows.

Example:
Button button = new Button("Click Me");
Here, `button` is an event source.

3. What are Event Classes in Java?


Answer: Event classes in Java are part of the `java.awt.event` package and represent the different types
of events. Common event classes include `ActionEvent`, `MouseEvent`, and `KeyEvent`.

Example:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
Here, `ActionEvent` is an event class.

4. What are Event Listeners in Java?


Answer: Event listeners are interfaces in the `java.awt.event` package that receive and process events.
Examples include `ActionListener`, `MouseListener`, and `KeyListener`.

Example:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked!");
}
});
Here, an `ActionListener` is added to a button to handle click events.

5. What is the Delegation Event Model in Java?


Answer: The delegation event model is a design pattern in Java where an event source delegates the
handling of an event to an event listener. The event listener is registered with the event source.

Example:
button.addActionListener(listener);
Here, `listener` is registered with `button` to handle events.

6. How do you handle Mouse Events in Java?


Answer: Mouse events can be handled using the `MouseListener` interface.

Example:
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse clicked!");
}
});
Here, `MouseAdapter` is used to handle mouse click events.

7. How do you handle Keyboard Events in Java?


Answer: Keyboard events can be handled using the `KeyListener` interface.

Example:
textField.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
System.out.println("Key pressed: " + e.getKeyChar());
}
});
Here, `KeyAdapter` is used to handle key press events.

8. What are Adapter Classes in Java?


Answer: Adapter classes provide default implementations of event listener interfaces, allowing you to
override only the methods you need.

Example:
textField.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse clicked!");
}
});

Here, `MouseAdapter` is used to handle mouse click events without implementing all methods of
`MouseListener`.

The AWT Class Hierarchy

9. What is the AWT Class Hierarchy?


Answer: The AWT class hierarchy is a set of classes provided by the Abstract Window Toolkit (AWT) in
Java for creating GUI applications.

User Interface Components

10. How do you create a Label in AWT?


Answer: You can create a label using the `Label` class.

Example:
Label label = new Label("Hello, World!");

11. How do you create a Button in AWT?


Answer: You can create a button using the `Button` class.

Example:
Button button = new Button("Click Me");

12. How do you create a Canvas in AWT?


Answer: You can create a canvas using the `Canvas` class.

Example:
Canvas canvas = new Canvas();

13. How do you create Scrollbars in AWT?


Answer: You can create scrollbars using the `Scrollbar` class.

Example:
Scrollbar scrollbar = new Scrollbar();

14. How do you create Text Components in AWT?


Answer: Text components include `TextField` and `TextArea`.

Example:
TextField textField = new TextField("Enter text");

15. How do you create a Checkbox in AWT?


Answer: You can create a checkbox using the `Checkbox` class.

Example:
Checkbox checkbox = new Checkbox("Check me");

16. How do you create Checkbox Groups in AWT?


Answer: You can create a group of checkboxes using the `CheckboxGroup` class.

Example:
CheckboxGroup group = new CheckboxGroup();
Checkbox checkbox1 = new Checkbox("Option 1", group, false);
Checkbox checkbox2 = new Checkbox("Option 2", group, true);

17. How do you create Choices in AWT?


Answer: You can create a choice using the `Choice` class.

Example:
Choice choice = new Choice();
choice.add("Option 1");
choice.add("Option 2");

18. How do you create Lists in AWT?


Answer: You can create a list using the `List` class.

Example:
List list = new List();
list.add("Item 1");
list.add("Item 2");

19. How do you create Panels in AWT?


Answer: You can create a panel using the `Panel` class.

Example:
Panel panel = new Panel();

20. How do you create a ScrollPane in AWT?


Answer: You can create a scrollpane using the `ScrollPane` class.

Example:
ScrollPane scrollPane = new ScrollPane();

21. How do you create Dialogs in AWT?


Answer: You can create a dialog using the `Dialog` class.

Example:
Dialog dialog = new Dialog(frame, "Dialog", true);

22. How do you create a MenuBar in AWT?


Answer: You can create a menubar using the `MenuBar` class.

Example:
MenuBar menuBar = new MenuBar();

Layout Managers

23. What are Layout Managers in AWT?


Answer: Layout managers are used to arrange components in a container.

24. How does BorderLayout work in AWT?


Answer: `BorderLayout` arranges components in five regions: North, South, East, West, and Center.

Example:
frame.setLayout(new BorderLayout());
frame.add(new Button("North"), BorderLayout.NORTH);
frame.add(new Button("Center"), BorderLayout.CENTER);

25. How does GridLayout work in AWT?


Answer: `GridLayout` arranges components in a grid of cells.

Example:
frame.setLayout(new GridLayout(2, 2));
frame.add(new Button("Button 1"));
frame.add(new Button("Button 2"));

26. How does FlowLayout work in AWT?


Answer: `FlowLayout` arranges components in a left-to-right flow.

Example:
frame.setLayout(new FlowLayout());
frame.add(new Button("Button 1"));
frame.add(new Button("Button 2"));
```

27. How does CardLayout work in AWT?


Answer: `CardLayout` allows you to flip through different components like a deck of cards.

Example:
CardLayout cardLayout = new CardLayout();
Panel panel = new Panel(cardLayout);
panel.add(new Button("Card 1"), "Card1");
panel.add(new Button("Card 2"), "Card2");

28. How does GridBagLayout work in AWT?


Answer: `GridBagLayout` is a flexible layout manager that aligns components vertically and horizontally
without requiring them to be of the same size.

Example:

GridBagLayout gridBagLayout = new GridBagLayout();


GridBagConstraints constraints = new GridBagConstraints();
frame.setLayout(gridBagLayout);
constraints.gridx = 0;
constraints.gridy = 0;
frame.add(new Button("Button 1"), constraints);
constraints.gridx = 1;
frame.add(new Button("Button 2"), constraints);

You might also like