In this tutorial we are providing you an example which illustrates you how to a create calculator in Swing with the source code and screen shot.
For developing a small calculator program in swing we need two different classes
The SwingCalculator.java calls the Calculator.java class by JFrame frame = new Calculator(). All the methods and actions are to be performed in Calculator.java class.
Calculator Code in Java Swing
Please save the code as SwingCalculator.java
Here is the code of Calculator.java
The constructor new CalculatorOp() calls the CalculatorOp class. The Swing component JTextField is used to create textbox on which calculation is to be performed. JPanel arranges the numeric buttons in a panel. JButton is used to perform an action. OperatorListener class is called to perform action on operators, i.e, '+,-,*,/,='. The class NumberListener is called for numbers 0 to 9.
Output will be displayed as:
If you enjoyed this post then why not add us on Google+? Add us to your Circles
Liked it! Share this Tutorial