UNB/ CS/ David Bremner/ teaching/ java/ RPNCalc3.java
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.BoxLayout;
public class RPNCalc3 extends DBFrame implements KeyPadClient, 
                                                 HistoryPaneClient{

    private JLabel resultLabel=new JLabel("results");

    private PromptField input;
    private RPNStack stack=new RPNStack();
    
    private HistoryPane results;

    private final String labels[][]=
            { {"7","8","9"}, 
              {"4","5","6"},
              {"1","2","3"},
              {"0",".", "Enter"},
              {"+","-","*"},
              {"/","clear","quit" }};

    public void mouseClickCallback(String text){
        input.setText(text);
    }

    public void keypressCallback(String text){
        if (text.equals("clear")){
            input.setText("");
            return;
        }
        if (text.equals("quit")){
            System.exit(0);
        }
            
        if ('0' <= text.charAt(0) && 
            text.charAt(0) <= '9' || text.charAt(0)=='.'){           
            input.setText(input.getText() + text);
        } else{
            process(text);
        }
    }

    private void process(String text){
        String result,operand;
            
        operand=input.getText();
        if (operand.equals(""))
            operand=(String)stack.top();
        
        stack.push(operand);
        result=stack.process(text);
        results.addElement(result);
        input.setText("");
    }
    public RPNCalc3(){
        super(new BorderLayout());
        results=new HistoryPane("123456789.892343");
        results.addClient(this);

        JPanel rightGroup=new JPanel(new BorderLayout());
        rightGroup.add(resultLabel,"North");
        rightGroup.add(results,"Center");
        getContentPane().add(rightGroup,"East");


        JPanel leftGroup=new JPanel();
        leftGroup.setLayout(new BoxLayout(leftGroup, BoxLayout.Y_AXIS));

        input=new PromptField("input",20);
        leftGroup.add(input);

        leftGroup.add(new KeyPad(labels,this));

        getContentPane().add(leftGroup,"West");

        getContentPane().add(new JLabel("RPN Calculator"),"North");
        pack();
        setVisible(true);
        
        
    }
    public static void main(String [] args){
        RPNCalc3 r=new RPNCalc3();
        System.out.println(r.getPreferredSize());
    }
}
//