/* IKE Calculator Revamped 6/17/01 */ import java.awt.*; import java.applet.*; import java.awt.event.*; public class Calculator extends Applet implements ActionListener { // variables public TextField input1, input2, display1; public Button b1, b2, b3, b4, b5, b6; public Panel p1, p1a, p1b, p2; public Color newColor = new Color(124, 151, 240); public void init() { p1 = new Panel(new GridLayout(2, 1, 5, 5)); p1a = new Panel(new GridLayout(1, 2, 5, 5)); p1b = new Panel(new GridLayout(1, 1, 5, 5)); p2 = new Panel(new GridLayout(2, 3, 5, 5)); setLayout(new GridLayout(2, 1, 5, 5)); setBackground(Color.blue); input1 = new TextField(); input1.setBackground(Color.white); input2 = new TextField(); input2.setBackground(Color.white); display1 = new TextField(); display1.setEditable(false); display1.setBackground(Color.white); b1 = new Button("+"); b1.setBackground(newColor); b2 = new Button("-"); b2.setBackground(newColor); b3 = new Button("C"); b3.setBackground(newColor); b4 = new Button("*"); b4.setBackground(newColor); b5 = new Button("/"); b5.setBackground(newColor); b6 = new Button("%"); b6.setBackground(newColor); p1a.add(input1); p1a.add(input2); p1b.add(display1); p1.add(p1a); p1.add(p1b); p2.add(b1); p2.add(b2); p2.add(b3); p2.add(b4); p2.add(b5); p2.add(b6); // add panels add(p1); add(p2); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); b6.addActionListener(this); } public void actionPerformed(ActionEvent ae) { int num1, num2, answer = 0; String str; // check for nothing in textField str = input1.getText(); if (str.equals("")) num1 = 0; else num1 = Integer.parseInt(str); str = input2.getText(); if (str.equals("")) num2 = 0; else num2 = Integer.parseInt(str); str = ae.getActionCommand(); // clear if (str.equals ("C")) { display1.setText(""); input1.setText(""); input2.setText(""); } else // do the math { if (str.equals ("+")) answer = num1 + num2; else if (str.equals ("-")) answer = num1 - num2; else if (str.equals ("*")) answer = num1 * num2; else if (str.equals ("/")) answer = num1 / num2; else if (str.equals ("%")) answer = num1 % num2; // display the result // display1.setText(Double.toString(answer)); display1.setText("Result = " + answer); } repaint(); } }