Source:
// Question 1.24
import java.applet.Applet;
import java.awt.event.*;
import java.awt.*;
public class q124 extends Applet
implements ActionListener
{
Label CashStr;
TextField CashTxt;
Button Clear;
float total = 0, cash;
int clear;
public void init()
{
CashStr = new Label ("Amount of cash collected");
add(CashStr);
CashTxt = new TextField (6);
CashTxt.addActionListener(this);
add(CashTxt);
Clear = new Button ("Clear value");
Clear.addActionListener(this);
add(Clear);
}
public void paint(Graphics g)
{
if (clear == 1)
total = 0;
else
total = total + cash;
g.drawString ("The total is: " + total, 50,90);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == Clear)
clear = 1;
else if (e.getSource() == CashTxt) {
clear = 0;
cash = Float.valueOf(CashTxt.getText()).floatValue(); }
repaint();
}
}