import java.awt.*;
import java.applet.*;


public class StackApplet extends Applet
{
        Stack s;
        Button bPush, bPop;
        TextField tf;
        List l;
        public void init()
        {
        s = new Stack(100);
        bPush = new Button("Push text field onto stack");
        bPop = new Button("Pop stack into text field");
        tf = new TextField();
        l = new List();

        setLayout(new GridLayout(6, 1));
        add(new Label("Type some text in here"));
        add(tf);
        add(bPush);
        add(bPop);
        add ( new Label("Below is a listbox that shows the contents of the stack"));
        add(l);

        }

        public boolean handleEvent(Event e)
        {
                String localString;
                if (e.target == bPush)
                {
                        s.push(tf.getText());
                        l.addItem(tf.getText(), 0);
                        return true;

                }
                else if (e.target == bPop)
                {
                        try{
                                localString = s.pop().toString();
                                l.delItem(0);
                        tf.setText(localString);
                        } catch (Exception ex) {System.out.println("Exception occurred: " + ex.toString());}
                        return true;
                }
                else
                {
                        return super.handleEvent(e);
                }

        }

}

