APPLET
APPLET
An applet is a small program that runs in a web browser or applet viewer. Applets
extend the java.applet.Applet class or javax.swing.
JApplet (for Swing-based applets). They were initially designed for creating dynamic
content for web pages but have been deprecated in modern Java versions.
Key Features
1. No main() Method: Unlike regular Java applications, applets are not standalone
programs. The browser or applet viewer invokes lifecycle methods to control
their execution.
2. Predefined Lifecycle: The applet lifecycle involves specific methods (init(),
start(), stop(), destroy()), which you override to customize behavior.
3. Graphical User Interface (GUI): Applets are capable of creating graphical
interfaces using AWT or Swing.
4. Security Restrictions: Applets run in a sandbox environment and cannot access
local system resources (e.g., files) for security reasons.
1. init():
o Called once during the initialization phase.
o Used for applet setup (e.g., loading resources, initializing variables).
2. start():
o Called every time the applet becomes active (e.g., when the web page
containing the applet is loaded or refreshed).
o Used to start animations or threads.
3. paint(Graphics g):
o Called whenever the applet needs to redraw itself.
o Used for rendering graphics or text.
4. stop():
o Called when the applet becomes inactive (e.g., user navigates away from
the page).
o Used to stop animations or release resources.
5. destroy():
o Called once when the applet is being removed from memory.
o Used for cleanup tasks.
APPLET PROGRAMME:-
import java.applet.Applet;
import java.awt.Graphics;
Modern Alternatives
1. Model:
Example:
In a banking application, the Model would represent the bank account, handling operations
like deposit, withdrawal, and balance calculation.
2. View:
Example:
In the banking application, the View would show the balance, display buttons for
deposit/withdrawal, and show error messages.
3. Controller:
Example:
In the banking application, the Controller would manage events like button clicks (Deposit or
Withdraw) and call the appropriate Model methods to update the account balance.
AWT: window fundamentals
AWT is a set of APIs provided by Java to create graphical user interfaces (GUIs).
AWT is part of Java’s standard library, and it provides basic UI components (like buttons, text fields,
and labels), event handling, and window management.
A Frame is a top-level window that can contain other components like buttons, text fields,
etc. It is used to represent the main window of the application.
In AWT, you create a frame using the Frame class, which is a subclass of Window.
Creating a frame in AWT (Abstract Window Toolkit) is simple. The Frame class in AWT is
used to create a basic window for a graphical user interface (GUI).
import java.awt.Frame;
frame.setVisible(true);
// Step 4: Close the Frame when the close button is clicked
frame.addWindowListener(new java.awt.event.WindowAdapter()
});
1. import java.awt.Frame;
o This imports the Frame class from the java.awt package.
2. Frame frame = new Frame("My First Frame");
o Creates a new frame window with the title "My First Frame."
3. frame.setSize(400, 300);
o Sets the width and height of the frame to 400x300 pixels.
4. frame.setVisible(true);
o Makes the frame visible on the screen. Frames are not visible by
default.
5. frame.addWindowListener(...)
o Adds a listener to detect the event when the user clicks the close
button (the "X" in the top-right corner).
6. System.exit(0);
o Ends the program when the window is closed.
1. new java.awt.event.WindowAdapter()
Purpose: This method is called automatically when the window is being closed
(e.g., when the user clicks the close button (X)).
What it does: Inside this method, you specify what should happen when the
close button is clicked. In most cases, you'll call System.exit(0) to terminate the
program.
2. Adding Components to a Frame
AWT components like buttons, text fields, and labels can be added to a frame using the
add() method.
import java.awt.*;
// Create a button
frame.add(button);
frame.setSize(400, 300);
frame.addWindowListener(new java.awt.event.WindowAdapter() {
});
frame.setVisible(true);
}
Write a program in JAVA to implement GUI in AWT
import java.awt.*;
import java.awt.event.*;
What It Does
1. button.addActionListener(...)
o Attaches an event listener to the button.
o It listens for click events on the button.
2. new ActionListener() { ... }
o Creates an anonymous inner class that implements the
ActionListener interface.
o The ActionListener interface has a single method:
actionPerformed(ActionEvent e).
3. public void actionPerformed(ActionEvent e)
o This method is executed whenever the button is clicked.
o Parameter ActionEvent e: Contains details about the event, such
as which button was clicked.
4. System.out.println("Button clicked!");
o The action to be performed when the button is clicked.
o In this case, it simply prints "Button clicked!" to the console.