import java.awt.*; import java.awt.event.*; import java.applet.*; /** * This is an applet for demonstrating the CalcLexer and CalcParser classes. We present * a graphical interface that allows the user to enter an expression, and shows the result * of parsing and evaluating that expression. This can be run either as an * applet or as an application. */ public class CalcDemo extends Applet implements ActionListener { /** * This Panel contains our window's content, whether we are running * as an applet or as an application. */ private Panel content; /** * A Label for the result of evaluating the last expression. */ private Label resultLabel; /** * The TextField in which expressions are entered. */ private TextField expressionField; /** * CalcDemo no-arg constructor. We initialize the content Panel with * all the elements it contains: one field and two labels. Note that * we may or may not be running as an applet: if we are an application, all * the Applet stuff we inherited will be unused, but our own instance * variables must still be initialized. */ public CalcDemo() { // First, create the Labels, and the TextField where the user will // enter expressions. Label expressionLabel = new Label("Enter expression: ",Label.RIGHT); expressionField = new TextField(20); expressionField.addActionListener(this); resultLabel = new Label("",Label.CENTER); // Create an inner Panel for the Label/TextField line. Panel top = new Panel(); top.add(expressionLabel); top.add(expressionField); // Finally, our content Panel: we will use a BorderLayout with the Label/TextField // line in the north border, and the result Label in the south. content = new Panel(new BorderLayout()); content.add(top,BorderLayout.NORTH); content.add(resultLabel,BorderLayout.SOUTH); } /** * Handle an ActionEvent. This is a completed text * entry in one of our TextFields (that is, return was pressed). * We call update to handle the new text. * @param e the relevant ActionEvent */ public void actionPerformed(ActionEvent e) { if (e.getSource()==expressionField) { CalcParser parser = new CalcParser(expressionField.getText()); String error = parser.getErrorMessage(); if (error==null) { // if no error, display value in black resultLabel.setText(parser.getValue()); resultLabel.setForeground(Color.black); } else { // if error, display it in red resultLabel.setText(error); resultLabel.setForeground(Color.red); } } } /** * The Applet init method, called when we are running as an Applet and are * loaded into the browser or applet viewer. Add our content Panel to our * inherited Applet panel. */ public void init() { add(content); } /** * The main method. Run as an application, we create * a Frame of our own (a window with the title "Calc Demo"), * and place the demo content panel inside it. * @param s a String[] containing command line args, which we ignore */ public static void main(String[] s) { // Construct a CalcDemo object. We will not use the Applet // part, but we will use the other parts (demo.content and all that). CalcDemo demo = new CalcDemo(); // Construct the window Frame, and add a listener so we will // exit when the window is closed. Frame frame = new Frame("Calc Demo"); frame.addWindowListener( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); // Add our content Panel to the Frame, pack it, and make it visible. frame.add("Center", demo.content); frame.pack(); frame.setVisible(true); } }