Question 4

(a) Write an applet that contains three buttons OK,CANCEL and HELP and one text field. if OK is pressed shown on the status bar-“OK is pressed” and the text field should turn red. When CANCEL is pressed -shown on the status bar-“ CANCEL is pressed “and text field should turn green. When HELP is pressed- shown on the status bar-“HELP is pressed” and the text field should turn yellow.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="AppletButtonActionEventH2E" width=300 height=300></applet>
*/
public class AppletButtonActionEventH2E extends Applet implements ActionListener
{
Button ok,cancel,help; TextField t;
String msg = "";
public void init()
{
ok = new Button("OK");
cancel = new Button("CANCEL");
help = new Button("HELP"); t = new TextField();
add(ok);
add(cancel);
add(help); add(t);
ok.addActionListener(this);
cancel.addActionListener(this);
help.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str = ae.getActionCommand();
if(str.equals("OK"))
{
msg = "OK is pressed"; t.setBackground(Color.RED);
}
else if(str.equals("CANCEL"))
{
msg = "CANCEL is pressed"; t.setBackground(Color.GREEN);
}
else
{
msg = "HELP is pressed";
}
repaint();
}
public void paint(Graphics g)
{
showStatus(msg); t.setBackground(Color.YELLOW);
}
}
(b) Explain Applet life cycle with demo program.
import java.awt.*;
import java.applet.*;
/*
<applet code="AppletLifeCycle" width=300 height=300>
</applet>
*/
public class AppletLifeCycle extends Applet
{
String msg1,msg2,msg3,msg4;
// called first
public void init()
{
// Initialization
msg1="init section begins";
}
// called second
public void start()
{
// start or resume execution
msg2="start section";
}
// called when applet stopped
public void stop()
{
// suspends execution
msg3="stop section";
}
// called when applet
public void destroy()
{
// execute shutdown activities
msg4="destroy section";
}
public void paint(Graphics g)
{
// redisplay content of window
g.drawString("Demo for basic methods of applet",20,40);
g.drawString(msg1,20,80);
g.drawString(msg2,20,90);
g.drawString(msg3,20,100);
g.drawString(msg4,20,120);
}
}

Make Comments..!!


Anand Patel
in Q-4(a) where is the textfield ?
incomplete code.......
Like · Comment ·
Admin and Joydip Panchal like this
Admin
Solved!!!
Like · 1 ·
Download Android App