Apple Ts
Apple Ts
Apple Ts
Applet: A small Java program that can be inserted into a web page and
run by loading that page in a browser.
– Applets don’t use the main() method, but when they are loaded, automatically
call certain methods (init, start, paint, stop, destroy).
– They are embedded inside a web page and executed in browsers.
– Takes input through Graphical User Input ( GUI ).
– They cannot read from or write to the files on local computer.
– They cannot run any programs from the local computer.
The above restrictions ensures that an Applet cannot do any damage to the local
system.
Life cycle of an Applet
init()
Begin Born
stop()
start()
Running Idle
destroy()
paint() start()
Dead End
Life cycle of an Applet
It is important to understand the order in which these
methods are called.
Initialisation
The init( ) method is the first method to be called.
This is where you should initialize variables.
This method is called only once during the run time of your applet.
At this point, you should free up any resources the applet may be using.
The stop( )
method is always called before destroy( ).
Building Applet Code: An Example
import java.awt.*;
import java.applet.Applet;
public class SimpleApplet extends Applet {
public void paint(Graphics g) {
g.drawString (“A Simple Applet",20, 20);
}
}
After you create this file, open your browser and then load this file, which
causes SimpleApplet to be executed.
width and height specify the dimensions of the display used by the applet.
Executing by using appletviewer
There are two ways
1. Use earlier html page, which contains applet tag, then execute by using
following command.
C:\>appletviewer htmlfilename.html
2. Include a comment at the beginning of your source code file that
contains the applet tag, then start applet viewer with your java source
code file. C:\>appletviewer SimpleApplet.java
import java.awt.*;
import java.applet.Applet;
/* <applet code=“SimpleApplet” width=200 height=60 ></applet> */
public class SimpleApplet extends Applet {
public void paint(Graphics g) {
g.drawString (“A Simple Applet",20, 20);
}
}
Four of these methods init(), start(), stop(), and destroy() are
defined by Applet.
tf=new JTextField();
tf.setBounds(30,40,150,20);
b=new JButton("Click");
b.setBounds(80,150,70,40);
add(b);add(tf);
b.addActionListener(this);
setLayout(null);
}
Called as stand-alone
Requires some third party
application as application
Nature tool help like a browser to
can be executed from
execute
command prompt