An applet is a small Java program that runs inside a web browser or applet viewer.
👉 Used for:
main() methodApplet execution follows a sequence of methods:
| Method | Purpose |
|---|---|
init() |
Initialize applet |
start() |
Start or resume |
paint(Graphics g) |
Display output |
stop() |
Pause execution |
destroy() |
Cleanup |
init()start()paint()stop()destroy()import java.applet.Applet;
import java.awt.Graphics;
public class MyApplet extends Applet {
public void paint(Graphics g) {
g.drawString("Hello Applet", 50, 50);
}
}
<html>
<body>
<applet code="MyApplet.class" width="300" height="300"></applet>
</body>
</html>
📊 Applet Life Cycle Diagram:
init() → start() → paint() → stop() → destroy()Swing is a modern Java GUI toolkit used to create rich desktop applications.
👉 It is part of:
| Component | Description |
|---|---|
JFrame |
Main window |
JButton |
Button |
JLabel |
Text display |
JTextField |
Input field |
JCheckBox |
Selection |
JTextArea |
Multi-line text |
import javax.swing.*;
public class SwingDemo {
public static void main(String[] args) {
JFrame f = new JFrame("Swing Example");
JButton b = new JButton("Click");
b.setBounds(100, 100, 100, 40);
f.add(b);
f.setSize(300, 300);
f.setLayout(null);
f.setVisible(true);
}
}
| Feature | AWT | Swing |
|---|---|---|
| Components | Heavyweight | Lightweight |
| Look | OS dependent | Customizable |
| Flexibility | Less | More |
Swing uses same event handling model:
import javax.swing.*;
import java.awt.event.*;
public class ButtonClick {
public static void main(String[] args) {
JFrame f = new JFrame();
JButton b = new JButton("Click");
b.setBounds(100, 100, 100, 40);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Clicked!");
}
});
f.add(b);
f.setSize(300, 300);
f.setLayout(null);
f.setVisible(true);
}
}
Swing loosely follows:
JFrame as main containeradd()| Feature | Applet | Swing |
|---|---|---|
| Type | Web-based | Desktop GUI |
| Execution | Browser | Standalone |
| Modern Use | Obsolete | Widely used |
| Components | AWT-based | Advanced |
main()init → start → paint → stop → destroyJFrame, JButtonOpen this section to load past papers