U05-01-JApplets & AWT Introduction
U05-01-JApplets & AWT Introduction
Dhyaneswaran J
AP - IT
Applet Introduction
• Applets are small applications accessed on an Internet server
• run as part of a Web document
• Applets have a user interface and different program structure
Example
import java.awt.*;
import java.applet.*;
public class SimpleApplet extends Applet {
public void paint(Graphics g) {
g.drawString("A Simple Applet", 20, 20);
}
}
Apple Creation
• It needs two packages java.awt.*,java.applet.* imported
• awt provides Window or Graphical based UI’s
• applet package consists of Applet class (inheritance)
• Applet classes are declared as public
• paint( ) method invoked to create or redraw applet on screen
• Provided by AWT and is overridden in applets
• Graphics context is used as a parameter in paint method
• Key Points
• Applets do not need a main( ) method
• Applets use interface provided by AWT classes
Applet – HTML
• Creation of Applet Tag in a HTML file
<applet code="SimpleApplet" width=200 height=60>
</applet>
• Run HTML file created using Web browser
• Use appletviewer command along with HTML file to run applet
C:\>appletviewer RunApp.html
Faster way
• Applet creation by adding Applet tag in comment line of our JAVA
program
• Run using appletviewer
Applet - Architecture
• Applet Architecture is event driven
• AWT notifies Applet about an event via event handlers
• Event Handlers are provided by Applets
• Applet makes quick response and returns control to AWT
• Applet Life Cycle contains four methods
• init( )
• start( )
• stop( )
• destroy()
• paint( )
• All the variables & member methods of Applet class are inherited
into JApplet class
import javax.swing.*;
import java.awt.event.*;
public class AppletEx extends JApplet implements ActionListener {
JButton b;
JTextField tf;
JLabel jl, jl1;
public void init() {
jl = new JLabel("Enter Text");
jl.setBounds(30, 10, 100, 100);
tf = new JTextField();
tf.setBounds(30, 100, 150, 20);
b = new JButton("Click");
b.setBounds(80, 150, 70, 40);
jl1 = new JLabel("");
jl1.setBounds(100, 180, 100, 100);
add(b);add(tf);
add(jl);add(jl1);
b.addActionListener(this);
setLayout(null);
setSize(300,300);
}
public void actionPerformed(ActionEvent e) {
jl1.setText(tf.getText());
}
}
AWT Classes
• AWT classes are available in java.awt.* package
• Classes are arranged in Hierarchical order and easy to understand its
purpose
• AWT classes defines windows functionality at each level
• Two most common classes are
• Panel – used by applets
• Frame – Standard window
• Component is abstract class and top level class in AWT hierarchy
• All attributes are visual components displayed on screen
• Component class maintains methods for managing events of mouse
and keyboard
• Component class remembers foreground, background and current
text font
SWING Vs. AWT
• AWT doesn't support pluggable look and • Swing supports pluggable look and feel.
feel.