0% found this document useful (0 votes)
30 views3 pages

Java Applets and AWT Event Handling Guide

Unit 4 covers Java Applets, Event Handling, and AWT, detailing the structure and lifecycle of applets, including methods like init(), start(), stop(), and destroy(). It explains event handling through the Delegation Event Model, with examples of event listener interfaces and AWT components for GUI applications. Additionally, it discusses graphics, colors, fonts, layout managers, and menus in AWT, providing code examples for practical understanding.

Uploaded by

joshihitesh1104
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)
30 views3 pages

Java Applets and AWT Event Handling Guide

Unit 4 covers Java Applets, Event Handling, and AWT, detailing the structure and lifecycle of applets, including methods like init(), start(), stop(), and destroy(). It explains event handling through the Delegation Event Model, with examples of event listener interfaces and AWT components for GUI applications. Additionally, it discusses graphics, colors, fonts, layout managers, and menus in AWT, providing code examples for practical understanding.

Uploaded by

joshihitesh1104
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

Java: Applets, Event Handling and AWT (Unit 4)

Unit 4: Applets, Event Handling, and AWT

1. Applet Basics:
- Applet is a small Java program that runs in a web browser.
- It is a subclass of [Link] or [Link].

Basic Structure:
import [Link].*;
import [Link].*;
public class MyApplet extends Applet {
public void paint(Graphics g) {
[Link]("Hello Applet", 20, 20);
}
}
(Use <applet code="[Link]" width=300 height=300></applet> in HTML)

2. Applet Architecture:
- init(): called once when applet is loaded.
- start(): called after init and every time applet is reloaded.
- stop(): called when applet is stopped.
- destroy(): called when applet is removed from memory.
- paint(Graphics g): used to display output.

3. Applet Display Methods:


- repaint(): calls update(), which then calls paint().
- drawString(), drawLine(), drawRect() etc. used for drawing.

4. Passing Parameters to Applets:


Use <param> tag in HTML:
<applet code="[Link]" width=300 height=300>
<param name="msg" value="Hello from HTML">
</applet>

Access with getParameter():


String str = getParameter("msg");

5. Event Handling - Delegation Event Model:


- Event source (button, textfield, etc) generates events.
- Event listener handles the event.
- Uses interfaces like ActionListener, MouseListener etc.
Java: Applets, Event Handling and AWT (Unit 4)

6. Event Classes:
- ActionEvent, MouseEvent, KeyEvent etc.
- Generated when a user interacts with GUI components.

7. Event Listener Interfaces:


- ActionListener (actionPerformed)
- MouseListener (mouseClicked, mouseEntered, etc)
- KeyListener (keyPressed, keyReleased, etc)

Example:
Button b = new Button("Click");
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link]("Button Clicked");
}
});

8. AWT - Abstract Window Toolkit:


Used for creating GUI applications.

- Windows: Frame, Dialog


- Controls: Button, Label, TextField, Checkbox, List

Example:
Frame f = new Frame("AWT Example");
Button b = new Button("Click");
[Link](50, 100, 80, 30);
[Link](b);
[Link](300, 300);
[Link](null);
[Link](true);

9. Graphics, Colors and Fonts:


- Graphics class used to draw shapes.
- setColor(Color c), setFont(Font f)
- Font f = new Font("Arial", [Link], 18);
- [Link](f); [Link]("Text", 50, 50);

10. Layout Managers:


- FlowLayout, BorderLayout, GridLayout etc.
Java: Applets, Event Handling and AWT (Unit 4)

- Automatically arrange components.

Example:
Frame f = new Frame("Layout Example");
[Link](new FlowLayout());
[Link](new Button("One"));
[Link](new Button("Two"));
[Link](200, 200);
[Link](true);

11. Menus:
- MenuBar, Menu, MenuItem used to create menus.

Example:
Frame f = new Frame("Menu Example");
MenuBar mb = new MenuBar();
Menu m = new Menu("File");
MenuItem mi = new MenuItem("Open");
[Link](mi);
[Link](m);
[Link](mb);
[Link](300, 300);
[Link](true);

Important Questions:
1. Explain the lifecycle of an Applet.
2. Write a program to display string using Applet.
3. How to pass parameters to an Applet? Write example.
4. Explain the Delegation Event Model.
5. Differentiate between Event Class and Listener Interface.
6. Write a program to handle button click using ActionListener.
7. Explain different AWT controls and layout managers with examples.
8. Write a program to create a Frame and add components using AWT.
9. What are Menus in AWT? Explain with code.

You might also like