Simple Calculator in Java Applet

Java Applet provides GUI way to create 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 program using applet.

  1. Simple applet- a beginner program
  2. Simple Calculator – with calculator functionality

1 Simple calculator program in java using applet

Here we use a simple Applet with two Input box to get Input and result is shown on next input box.

To perform Addition, Subtraction, Multiplication and Division.

Four buttons bill be provided.

Added Action Listener to each button.

Based on button click  public void actionPerformed(ActionEvent e) is get called and and based on event source appropriate if statement is executed.

Java applet program for calculator is as below

Below is the output of the calculator program in java

Simple Calculator in Java Applet
Java Applet Simple Calculator Example

Above Example is create to learn Basic Functionality.

2 Java applet program for calculator

This GUI looks like a real calculator. Here all values are input through button click.

Here We have created buttons for 0-9 input.

Operator buttons +, -, *, /, % and =.

Also provided button for AC, BSp, . (dot), and RSET (Reset).

Functions of these button are

AC– Clear the Input Text Field.

BSp– Backspace used to remove a character from input string.

dot(.)– Dot is used for decimal number.

RSET-Reset both text box input and result box.

All Above Features are included in Example.

Additional Feature of this calculator is that it can accept multiple input with different operators.

After adding all numbers it shows the result in top text box.

Simple Calculator in Java Applet
Simple Java Applet Calculator

Simple Calculator Code

  • Created Layout with two text box and numbers and special buttons like AC, BSp.
  • Add 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

In Java ScriptEngineManager is used for expression calculation.

That makes our calculation part easy.

Read More

  1. Login page in awt
  2. Applet Life Cycle in Java with Example