/** Program 6.6 Applet that Checks for Balanced Parentheses */ /* * This applet accepts a string typed by the user that contains parentheses * in the set '{','(','[','}',')',']' and checks it for parenthesis balance */ import java.applet.*; import java.awt.*; public class ParenMatchApplet extends Applet implements ActionListener { // declare the data fields as follows: Label instructionLabel, // gives the instructions for user input inputLabel, // labels the input text box outputLabel; // labels the output text box TextField inputField, // the input text box outputField; // the output text box ParenMatcher PM; // the object that computes results // the applet's methods follow /*----------------------------------------*/ public void init() { instructionLabel = new Label( "Type an input expression in the box below, and press the Enter key.", Label.CENTER); add(instructionLabel); // add the instruction label to // top of applet layout inputLabel = new Label(" input:"); add(inputLabel); // then, add the input label inputField = new TextField(40); // create a 40-column-wide // input field and add it to add(inputField); // the applet's layout inputField.addActionListener(this); // add the event handler outputLabel = new Label("output:"); // add the output label add(outputLabel); outputField = new TextField(40); // create a 40-column-wide // output field and add it to add(outputField); // the applet's layout inputField.requestFocus(); // put the blinking vertical text // insertion cursor in the input box PM = new ParenMatcher(); // create a new // parenthesis matcher object } // end init /*----------------------------------------*/ public void actionPerformed(ActionEvent e) { PM.setInput(e.getActionCommand());// send the input string to the // parenthsis matcher object, PM. PM.parenMatch(); // analyze parenthesis balance outputField.setText(PM.getOutput());// put result in output field } // end actionPerformed /*----------------------------------------*/ }// end applet