Simple Calculator in Java

Summary

Creating a simple graphical calculator to perform Addition, Subtraction, Multiplication & Division made up using swing components.

User has to input numbers as A & B and the output will be based on the two numbers given by user.

Select the appropriate operation & output is displayed below using JLabel.

The Class has been designed by extending JPanel which can be called from other classes and added onto the JFrame for displaying.

Screenshot

Simple Calculate in Java Screenshot

Code

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.rohansakhale.calc;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author RohanSakhale
*/
public class Calc extends JPanel {
JTextField tfInputA, tfInputB;
JLabel lInputA, lInputB, lAnswer;
JButton bAdd, bSub, bMult, bDiv;
JPanel inputA, inputB, operations, collection;
public Calc() {
tfInputA = new JTextField(15);
tfInputB = new JTextField(15);
lInputA = new JLabel("Enter Input A: ");
lInputB = new JLabel("Enter Input B: ");
lAnswer = new JLabel("Answer: ");
bAdd = new JButton(" + ");
bAdd.setSize(50, 20);
bSub = new JButton(" - ");
bSub.setSize(50, 20);
bMult = new JButton(" * ");
bMult.setSize(50, 20);
bDiv = new JButton(" / ");
bDiv.setSize(50, 20);
inputA = new JPanel();
inputB = new JPanel();
operations = new JPanel(new GridLayout(0, 2));
collection = new JPanel();
collection.setLayout(new BoxLayout(collection, BoxLayout.Y_AXIS));
CalcListener myListener = new CalcListener();
bAdd.addActionListener(myListener);
bSub.addActionListener(myListener);
bMult.addActionListener(myListener);
bDiv.addActionListener(myListener);
inputA.add(lInputA);
inputA.add(tfInputA);
inputB.add(lInputB);
inputB.add(tfInputB);
operations.add(bAdd);
operations.add(bSub);
operations.add(bMult);
operations.add(bDiv);
collection.add(inputA);
collection.add(inputB);
collection.add(operations);
collection.add(lAnswer);
add(collection);
}
class CalcListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (!tfInputA.getText().equals(") && !tfInputB.getText().equals(")) {
int a = Integer.parseInt(tfInputA.getText());
int b = Integer.parseInt(tfInputB.getText());
double answer = 0;
if (e.getSource() == bAdd) {
answer = a + b;
} else if (e.getSource() == bSub) {
answer = a - b;
} else if (e.getSource() == bMult) {
answer = a * b;
} else if (e.getSource() == bDiv) {
answer = a / b;
}
tfInputA.setText("");
tfInputB.setText("");
tfInputA.requestFocus();
lAnswer.setText("Answer: " + answer);
} else {
JOptionPane.showMessageDialog(null, "Please enter proper values", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
}
class CalculatorDemo{
public static void main(String[] args) {
JFrame calcFrame = new JFrame("Calculator Demo");
Calc calc = new Calc();
calcFrame.add(calc);
calcFrame.setSize(400, 300);
calcFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
calcFrame.setVisible(true);
}
}