Unit 3 Java
Unit 3 Java
Applet:-
An Applet is a small Java program that runs inside a web browser or an
applet viewer. It is part of Java AWT (Abstract Window Toolkit) and is
used to create interactive user interfaces on web pages.
Features of an Applet
1. Runs inside a browser or applet viewer
2.Does not have a main() method
3.Uses AWT for GUI components
4. Can respond to user input like mouse clicks and keyboard events
5.Uses paint(Graphics g) method to draw on the screen
2. Lifecycle of an Applet
An applet has five states:
import java.applet.Applet;
import java.awt.Graphics;
public class ParameterApplet extends Applet {
String message;
<html>
<body>
<applet code="ParameterApplet.class"
width="300" height="200">
<param name="msg" value="Hello, Applet!">
</applet>
</body>
</html>
The msg parameter is passed with the value "Hello, Applet!".
Compile and Run the Applet
HTML File:
<html>
<body>
<applet code="MultiParamApplet.class" width="300" height="200">
<param name="name" value="Alice">
<param name="age" value="25">
</applet>
</body>
</html>
This applet will display:
Hello, Alice!
Your Age: 25
Event Handling in Java
Event handling in Java allows interactive applications to respond to user
actions like clicking a button, typing in a text field, or moving the mouse.
It is widely used in GUI programming with AWT and Swing.
1. Basics of Event Handling
Java follows the "Event Delegation Model", which consists of:
public ButtonEventExample() {
setLayout(new FlowLayout());
setSize(300, 200);
setVisible(true);
}
public TextFieldEventExample() {
setLayout(new FlowLayout());
add(label);
add(textField);
textField.addActionListener(this); // Register
event listener
setSize(300, 200);
setVisible(true);
}
public MouseEventExample() {
setLayout(new FlowLayout());
setSize(300, 200);
setVisible(true);
}
public KeyEventExample() {
setLayout(new FlowLayout());
setSize(300, 200);
setVisible(true);
}