Simple Calculator in Java Applet

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.

  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 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

Below is the output of the calculator program in java

Simple Calculator in Java Applet
Java Applet Simple Calculator Example

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 in Java Applet
Simple Java Applet Calculator

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

In Java, ScriptEngineManager is used for expression calculation.

That makes our calculation part easy.

Read More

  1. JLabel in Java Swing
  2. JComboBox in Java Swing
  3. JTable in Java Swing
  4. JTable Pagination in Java JDBC
  5. Registration Form in Java Swing
  6. Login form in Java Swing