/* In this applet, the user can type in two real numbers. The user can click on buttons labled +, - , *, and / to perform basic arithmetic operations on the numbers. When the user clicks on a button the answer is displayed. The applet should be about 200 by 120 pixels. */ import java.awt.*; import java.awt.event.*; import java.applet.*; public class SimpleCalculator extends Applet implements ActionListener { TextField xInput, yInput; // Input boxes for the numbers. Label answer; // Label for displaying the answer, or an // error message if appropriate. public void init() { setBackground(Color.lightGray); /* Create the input boxes, and make sure that the background color is white. (On some platforms, it is automatically.) */ xInput = new TextField("0"); xInput.setBackground(Color.white); yInput = new TextField("0"); yInput.setBackground(Color.white); /* Create panels to hold the input boxes and lables "x =" and "y = ". By using a BorderLayout with the TextField in the Center position, the TextField will take up all the space left after the label is given its preferred size. */ Panel xPanel = new Panel(); xPanel.setLayout(new BorderLayout(2,2)); xPanel.add( new Label(" x = "), BorderLayout.WEST ); xPanel.add(xInput, BorderLayout.CENTER); Panel yPanel = new Panel(); yPanel.setLayout(new BorderLayout(2,2)); yPanel.add( new Label(" y = "), BorderLayout.WEST ); yPanel.add(yInput, BorderLayout.CENTER); /* Create a panel to hold the four buttons for the four operations. A GridLayout is used so that the buttons wil all have the same size and will fill the panel. */ Panel buttonPanel = new Panel(); buttonPanel.setLayout(new GridLayout(1,4)); Button plus = new Button("+"); plus.addActionListener(this); buttonPanel.add(plus); Button minus = new Button("-"); minus.addActionListener(this); buttonPanel.add(minus); Button times = new Button("*"); times.addActionListener(this); buttonPanel.add(times); Button divide = new Button("/"); divide.addActionListener(this); buttonPanel.add(divide); /* Create the label for displaying the answer (in red). */ answer = new Label("x + y = 0", Label.CENTER); answer.setForeground(Color.red); /* Set up the layout for the applet, using a GridLayout, and add all the components that have been created. */ setLayout(new GridLayout(4,1,2,2)); add(xPanel); add(yPanel); add(buttonPanel); add(answer); /* Try to give the input focus to xInput, which is the natural place for the user to start. */ xInput.requestFocus(); } // end init() public Insets getInsets() { // Leave some space around the borders of the applet. return new Insets(2,2,2,2); } public void actionPerformed(ActionEvent evt) { // When the user clicks a button, get the numbers // from the input boxes and perform the operation // indicated by the button. Put the result in // the answer label. If an error occurs, an // error message is put in the label. double x, y; // The numbers from the input boxes. /* Get a number from the xInput TextField. Use xInput.getText() to get its contents as a String. Convert this String to a double. The try...catch statement will check for errors in the String. If the string is not a legal number, the error message "Illegal data for x." is put into the answer and the actionPerformed() method ends. */ try { String xStr = xInput.getText(); Double d = new Double(xStr); x = d.doubleValue(); } catch (NumberFormatException e) { answer.setText("Illegal data for x."); return; } /* Get a number from yInput in the same way. */ try { String yStr = yInput.getText(); Double d = new Double(yStr); y = d.doubleValue(); } catch (NumberFormatException e) { answer.setText("Illegal data for y."); return; } /* Perfrom the operation based on the action command from the button. Note that division by zero produces an error message. */ String op = evt.getActionCommand(); if (op.equals("+")) answer.setText( "x + y = " + (x+y) ); else if (op.equals("-")) answer.setText( "x - y = " + (x-y) ); else if (op.equals("*")) answer.setText( "x * y = " + (x*y) ); else if (op.equals("/")) { if (y == 0) answer.setText("Can't divide by zero!"); else answer.setText( "x / y = " + (x/y) ); } } // end actionPerformed() } // end class SimpleCalculator