//— class StackDemo shows how to use the linear set of objects —//
import java.applet.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class StackDemo extends Applet {
//— Instance variables —//
Frame f;
//— Instance methods —//
public void init ()
{
f = new StackFrame (“Stack”); // instanciate a frame object
f.setSize (200, 150);
f.setResizable (false);
f.show ();
}
public void start ()
{
f.setVisible (true);
repaint ();
}
public void stop ()
{
f.setVisible (false);
}
public void paint (Graphics g)
{
g.drawString (“Ancestor object for the frame”, 0, 30);
}
}// end of class StackDemo
//— class StackFrame —//
class StackFrame extends Frame {
StackPanel p;
Stack stk;
MessageBox mb;
//— Constructor —//
public StackFrame (String title)
{
super (title);
//— The object to handle action events —//
ActionListener al = new ActionHandler ();
//— Instanciate a panel object and add it into the frame —//
p = new StackPanel (al);
add(BorderLayout.CENTER, p);
//— Instanciate a message box class —//
mb = new MessageBox (this, “Stack Message”, al);
//— Instanciate stack class —//
stk = new Stack ();
//— anonimous inner class for handling window events —//
addWindowListener (new WindowAdapter (){
public void windowClosing (WindowEvent e)
{
e.getWindow ().dispose ();
}
});
}
//— Inner private class StackPanel —//
private class StackPanel extends Panel {
//— Instance variables —//
Label lblTitle;
TextField tfStack;
Button btnPush, btnPop, btnPeek;
//— Constructor —//
public StackPanel (ActionListener al)
{
super ();
lblTitle = new Label (“Stack element”);
tfStack = new TextField (25);
btnPush = new Button (“Push”);
btnPop = new Button (“Pop “);
btnPeek = new Button (“Peek”);
add (lblTitle);
add (tfStack);
add (btnPush);
add (btnPop);
add (btnPeek);
btnPush.addActionListener (al);
btnPop.addActionListener (al);
btnPeek.addActionListener (al);
setBackground (Color.green);
}
}// end of class StackPanel
//— Inner class MessageBox —//
private class MessageBox extends Dialog {
Label lbl; // text to show in the dialog box
Button btnOK; // botton OK
//— Constructor —//
public MessageBox (Frame parent, String title, ActionListener al)
{
super (parent, title, true); //call the ancestor constructor and make the dialog box modal
setSize (100, 150);
setBackground (Color.red);
//— Change the layout and add components into the container —//
setLayout (new FlowLayout ());
add ( lbl = new Label ());
add ( btnOK = new Button (“OK”));
btnOK.addActionListener (al);
}
//— Methods —//
public void showBox (String s)
{
lbl.setText (s);
show ();
}
}// end class MessageBox
//— Inner class ActionHandler —//
private class ActionHandler implements ActionListener {
public void actionPerformed (ActionEvent e)
{
Object source = e.getSource ();
String s = p.tfStack.getText ();
if (source == p.btnPush)
{
//— If the tfStack is not empty than pust its content into the stack —//
if (!s.equals (“”))
stk.push (s);
p.tfStack.setText (“”);
System.out.println (“Push”);
p.tfStack.requestFocus ();
}
else if (source == p.btnPop)
{
//— If the stack is not empty, pop the element of the stack —//
if (!stk.empty ())
p.tfStack.setText ((String) stk.pop ());
else
{
p.tfStack.setText (“”);
mb.showBox (“Stack is empty”);
}
System.out.println (“Pop”);
p.tfStack.requestFocus ();
}
else if (source == p.btnPeek)
{
//— If the stack is not empty, pop the element of the stack —//
if (!stk.empty ())
p.tfStack.setText ((String) stk.peek ());
else
{
p.tfStack.setText (“”);
mb.showBox (“Stack is empty”);
}
System.out.println (“Peek”);
p.tfStack.requestFocus ();
}
else if (source == mb.btnOK)
{
mb.dispose ();
}
}
}// end of class ActionHandler
}// end of class StackFrame
