Professional Documents
Culture Documents
PhaseII Applet
PhaseII Applet
An applet is a window based java program that is used for internet applications. These
can be run by using either web browser or with an applet viewer.
1. Local applet
2. Remote applet
Local applet are developed locally and stored in the local computer. To run a local
applet there is no need for internet connection. At the run time the system searches the applet
code in the local hard disk.
Remote applets are developed by unknown persons and stored in remote computers. To
run a remote applet internet connection is needed. At the run time the system searches the applet
code in the internet and it is downloaded. (web address)
1. init()
This mehod is used to initialize the variables.
2. start()
Used to start the applet. It must be called after the stop() method.
3. stop()
Used to stop the applet.
4.destroy()
This mehod is used to deallocate the applet memory. It must be called after stop() method.
5. paint(Graphics g)
used to display the Output to applet window screen.
6.repaint()
This method is used to refresh to the applet windows. equal to clearscreen.
Restrictions of Applet:
import java.applet.*;
import java.awt.*;
public class AppletEg extends Applet
{
public void init()
{
System.out.println("Applet Initilized");
}
public void start()
{
System.out.println("Applet Started");
}
public void paint(Graphics g)
{
System.out.println("Applet Processed");
}
public void stop()
{
System.out.println("Applet Stopped");
}
public void destroy()
{
System.out.println("Applet Deallocated");
}
}
//<applet code="AppletEg" height=400 width=600></applet>
Label
TextField
Button
Choice
List
ScrollBar
Checkbox
Etc..,
import java.awt.*;
import java.applet.*;
public class Myawt extends Applet
{
Label l1,l2,l3,l4;
TextField t1,t2;
Button b1;
List l;
Checkbox c1,c2,c3,c4;
Choice cho;
CheckboxGroup g;
public void init()
{
l1=new Label("Name");
l2=new Label("Password");
l3=new Label("Permanent Employees");
l4=new Label("Gender");
t1=new TextField(20);
t2=new TextField(30);
t2.setEchoChar('@');
g=new CheckboxGroup();
c1=new Checkbox("Male",true,g);
c2=new Checkbox("Female",false,g);
c3=new Checkbox("Training Staff");
c4=new Checkbox("Exp Staff");
cho=new Choice();
b1=new Button("Click");
l=new List(3,true);
l.add("Nithya");
l.add("Anitha");
l.add("Saranya");
cho.add("Head Office");
cho.add("Branch Office");
add(l1);add(t1);add(l2);add(t2);add(b1);add(l3);add(l);add(l4);add(c1);add(c2);add(c3);
add(c4);add(cho);
}
}
//<applet code=Myawt height=600 width=600></applet>
Layouts
1. Flow layout
2. Border Layout
3. Grid Layout
4. Card Layout
Flow Layout
import java.awt.*;
import java.applet.*;
public class FlowDemo extends Applet
{
Button b1,b2,b3,b4;
public void init()
{
//setLayout(new FlowLayout(FlowLayout.CENTER));//default layout
FlowLayout ff=new FlowLayout(FlowLayout.RIGHT);
setLayout(ff);
b1=new Button("one");
b2=new Button("two");
b3=new Button("three");
b4=new Button("four");
add(b1);
add(b2);
add(b3);
add(b4);
}
}
Event Handling:
When user initiates an Event AWT generates (fires) corresponding event and appropriate
methods will be invoked automatically.
Types of Listener
1. Semantic Listener
Action Listener
Item Listener
Text Listener
Adjustment Listener
Semantic Listener:
// Action Listener
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Action extends Applet implements ActionListener
{
Button b1,b2;
TextField t1,t2,t3;
Label l1,l2;
public void init()
{
l1=new Label("Enter A value");
l2=new Label("Enter B value");
t1=new TextField(25);
t2=new TextField(25);
t3=new TextField(25);
b1=new Button("Add");
b2=new Button("Sub");
add(l1);
add(t1); add(l2);add(t2);
add(b1); add(b2);
add(t3); b1.addActionListener(this);
b2.addActionListener(this);
}