26, మార్చి 2012, సోమవారం

A CODE ABOUT THE APPLET PROGRAMMING WHICH INCLUDES THE AWT COMPONENTS AND LIFECYCLE OF AN APPLET


// <applet code=myappframe width=400 height=500></applet>
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class myappframe extends Applet implements ActionListener
{
Frame f;
boolean red=true;
public void init()
{
 f=new Frame();
f.setBackground(Color.red);
f.setBounds(100,100,300,300);
Button b1=new Button("click me");
f.add("North",b1);
b1.addActionListener(this);
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
f.setVisible(false);}});
f.setVisible(true);
}

public void actionPerformed(ActionEvent e)
{if(red)
{
f.setBackground(Color.blue);
red=false;
}
else
{
f.setBackground(Color.red);
red=true;
}}
public void start()
{
f.setVisible(true);
System.out.println("start");
}
public void paint(Graphics g)
{
g.drawString("hello",100,100);
System.out.println("paint");
}
public void stop()
{
f.setVisible(false);
System.out.println("stop");
}
public void destroy()
{
System.out.println("destroy");
}}


EXECUATION:-
STEP1:-To execute the above program save as myappframe.java after compile this code by using the standard procedure
E:\>javac myappframe.java
STEP2:- use here appletviewer with the same above source code Note:- here u don’t try to execute the DOT CLASS FILE directly
E:\>appletviewer myappframe.java
OUTPUT:-
start
paint
paint
paint
stop
destroy
 here 2 applet frames appears to u one have button if u click on this it will change its background color RED to BLUE 
2nd frame only displays the sample message  HELLO

 another program for illustration of applet concepts  it an exercise program u make u r self according to u r requirement
here just i am giving some idea



import java.awt.event.*;
import java.awt.*;
import java.applet.*;
import java.util.*;
/* <applet code=form width=400 height=400></applet>*/
public class form extends Applet implements ActionListener
{
int i;
Button b1,b2;
TextField tf;
int num=0,incr;


public void init()
{
b1=new  Button("inc");
b2=new Button("dec");
tf=new TextField();
b1.addActionListener(this);
b2.addActionListener(this);
add(b1);
add(b2);
add(tf);
}



public void start()
{
/*while(true)
{
try
{
num+=incr;
tf.setText(""+num);
Thread.sleep(1000);
}
catch(Exception e)
{}
}/*
}


public void actionPerformed(ActionEvent ae)      
{
String s=ae.getActionCommand();
if(s.equals("inc"))
{
incr=1;
start();
}
else
{
incr=-1;
start();
}
/*while(true)
{
String s=ae.getActionCommand();
try
{
while(true)
{
if(s.equals("inc"))
{
incr=1;
num+=incr;
tf.setText(""+num);
Thread.sleep(1000); 
if(s.equals("dec"))
break;
}
else
{
incr=-1;
num+=incr;
tf.setText(""+num);
Thread.sleep(1000); 
if(s.equals("inc"))
break;
}
}
}
catch(Exception e){}
}*/
}

/*public void repeat()
{
while(true)
{
num+=incr;
tf.setText(""+num);
Thread.sleep(1000);
if(s.equals("dec"))
}
}*/
}

కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి