A Calculator Example Program (With Java Code)

Technological Waste
Images Etc Ltd / Getty Images

The following Java code is for a simple calculator. There are nine JButtons to represent the numbers 1 to 9, and three JButtons for addition, subtraction and totaling the result. A JTextField at the top keeps track of the numbers being pressed and the result of the arithmetic operation.

Java Code

The purpose of this Java program is to show how to implement an ActionListener interface for handling JButton button event clicks by using the containing class, an inner class, and an anonymous inner class.