Java Applet provides a GUI way to create a program and its interface, Here we are creating Simple Calculator in Java Applet with GUI and its processing.
As we have already learned about applet and applet life cycle.
Here we have discussed two simple calculator programs using the applet.
- Simple applet- a beginner program
- Simple Calculator – with calculator functionality
1 Simple calculator program in java using applet
Here we use a simple Applet with two Input boxes to get Input and the result is shown on the next input box.
To perform Addition, Subtraction, Multiplication, and Division.
Four buttons will be provided.
Added Action Listener to each button.
Based on the button click public void actionPerformed(ActionEvent e) is get called and based on the event source appropriate if the statement is executed.
The Java applet program for the calculator is as below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="Calculator" width="700" height="200"> </applet>*/ public class Calculator extends Applet implements ActionListener { String msg = ""; TextField t1, t2, t3; Button b1, b2, b3, b4; Label l1, l2, l3; public void init() { l1 = new Label("First Number"); add(l1); t1 = new TextField(15); add(t1); l2 = new Label("Second Number"); add(l2); t2 = new TextField(15); add(t2); l3 = new Label("Result"); add(l3); t3 = new TextField(15); add(t3); b1 = new Button("ADD"); add(b1); b1.addActionListener(this); b2 = new Button("SUB"); add(b2); b2.addActionListener(this); b3 = new Button("MULT"); add(b3); b3.addActionListener(this); b4 = new Button("DIV"); add(b4); b4.addActionListener(this); } public void actionPerformed(ActionEvent e) { if (e.getSource() == b1) { int x = Integer.parseInt(t1.getText()); int y = Integer.parseInt(t2.getText()); int sum = x + y; t3.setText(" " + sum); } if (e.getSource() == b2) { int x = Integer.parseInt(t1.getText()); int y = Integer.parseInt(t2.getText()); int sub = x - y; t3.setText(" " + sub); } if (e.getSource() == b3) { int x = Integer.parseInt(t1.getText()); int y = Integer.parseInt(t2.getText()); int mul = x * y; t3.setText(" " + mul); } if (e.getSource() == b4) { int x = Integer.parseInt(t1.getText()); int y = Integer.parseInt(t2.getText()); int div = x / y; t3.setText(" " + div); } showStatus(" text & button example"); repaint(); } } |
Below is the output of the calculator program in java
The above example is created to learn Basic Functionality.
2 Java applet program for calculator
This GUI looks like a real calculator. Here all values are input through a button click.
Here We have created buttons for 0-9 input.
Operator buttons +, -, *, /, % and =.
Also provided a button for AC, BSp, . (dot), and RSET (Reset).
The functions of these buttons are
AC– Clear the Input Text Field.
BSp– Backspace used to remove a character from the input string.
dot(.)– Dot is used for decimal numbers.
RSET-Reset both text box input and result box.
All the Above Features are included in the Example.
An additional feature of this calculator is that it can accept multiple inputs with different operators.
After adding all numbers it shows the result in the top text box.

Simple Calculator Code
- Created Layout with two text boxes and numbers and special buttons like AC, and BSp.
- Add buttons to actionListener b[i].addActionListener(this);
- Create actionPerformed to get actionCommand()
- Define work for =, AC, BSp, and RESET
- On = arithmetic calculation should be done that is handled by
calculate()
- On calculate() use
ScriptEngineManager
for calculation
Calculator.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | import java.applet.Applet; import java.awt.Graphics; import java.awt.Button; import java.awt.Frame; import java.awt.TextField; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; public class Calculator extends Applet implements ActionListener { public static final String[] TEXT = {"AC", "BSp", "%", "/", "7", "8", "9", "*", "4", "5", "6", "-", "1", "2", "3", "+", "RSET", "0", ".", "="}; TextField t1; TextField t2; StringBuffer value = new StringBuffer(); public void init() { Frame title = (Frame) this.getParent().getParent(); title.setTitle("Simple Calculator"); setLayout(null); t1 = new TextField("", 4); t1.setBounds(50, 10, 200, 50); add(t1); t2 = new TextField("", 4); t2.setBounds(50, 60, 200, 50); add(t2); int x = 50; int y = 115; int k = 1; Button b[] = new Button[20]; for (int i = 0; i < TEXT.length; i++) { b[i] = new Button("" + TEXT[i]); b[i].setBounds(x * k, y, 50, 50); if (k % 4 == 0) { x = 50; y += 50; k = 0; } k++; add(b[i]); b[i].addActionListener(this); } } public void paint(Graphics g) { } public void actionPerformed(ActionEvent e) { String s = e.getActionCommand(); if (s.equalsIgnoreCase("=")) { System.out.println("generating result"); t1.setText(calculate(value)); value.setLength(0); } else if (s.equalsIgnoreCase("AC")) { System.out.println("AC: Resetting Input"); value.setLength(0); t2.setText(""); } else if (s.equalsIgnoreCase("BSp")) { System.out.println("BSp: Erasing a character"); value.setLength(value.length() - 1); t2.setText(value.toString()); } else if (s.equalsIgnoreCase("RSET")) { System.out.println("Reset: Resetting Input output "); value.setLength(0); t1.setText(""); t2.setText(""); } else { value.append(s); t2.setText(value.toString()); } } public String calculate(StringBuffer sb) { ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByName("JavaScript"); Object result = null; try { result = engine.eval(sb.toString()); System.out.println(result); } catch (ScriptException ex) { System.out.println("Exception : Check again your operator sequence"); result = "Error"; } return result.toString(); } } |
In Java, ScriptEngineManager is used for expression calculation.
That makes our calculation part easy.