Experiment 15: Write a Java Program to develop a GUI based Calculator application using the
Swing Package.
Aim
To design and implement a Calculator application using Java Swing that performs basic arithmetic
operations such as addition, subtraction, multiplication, and division using eventdriven
programming.
Theory
Java Swing is a part of the Java Foundation Classes (JFC) and is used to create
platformindependent Graphical User Interface (GUI) applications. Swing provides a rich set of
lightweight components such as JFrame, JButton, and JTextField that help in building interactive
desktop applications.
Algorithm
1. Start the program.
2. Create a JFrame to serve as the calculator window.
3. Add a JTextField to display numbers and results.
4. Create JButton objects for digits (0–9), operators (+, −, ×, ÷), clear (C), and equals (=).
5. Position all components using setBounds() and add them to the frame.
6. Register ActionListener for all buttons.
7. When a number button is clicked, append the digit to the text field.
8. When an operator button is clicked:
o Store the current value as the first operand. o Store the selected
operator.
o Clear the text field.
9. When the equals (=) button is clicked:
o Read the second operand. o Perform the arithmetic operation based on
the stored operator.
o Display the result in the text field.
10. If the clear (C) button is pressed, reset all values and clear the display.
________________________________________________________________________
vJKasi Janardhan
Venkata Reddy (92400118862)
Praveen(92400118839)
Program Code
import [Link].*;
import [Link].*;
import [Link].*;
public class CalculatorSwing extends JFrame implements ActionListener {
JTextField t1;
JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b0;
JButton bAdd, bSub, bMul, bDiv, bEq, bClr;
double num1 = 0, num2 = 0, result = 0;
char operator;
CalculatorSwing() {
setTitle("Calculator");
setSize(300, 400);
setLayout(null);
t1 = new JTextField();
[Link](20, 20, 250, 40);
[Link](false);
add(t1);
b1 = new JButton("1"); b2 = new JButton("2"); b3 = new JButton("3");
b4 = new JButton("4"); b5 = new JButton("5"); b6 = new JButton("6");
b7 = new JButton("7"); b8 = new JButton("8"); b9 = new JButton("9");
b0 = new JButton("0");
bAdd = new JButton("+");
bSub = new JButton("-");
bMul = new JButton("*");
bDiv = new JButton("/");
bEq = new JButton("=");
bClr = new JButton("C");
______________________________________________________________________________
v Kasi Janardhan Reddy (92400118862)
J Venkata Praveen (92400118839)
JButton[] buttons = {
b7, b8, b9, bDiv,
b4, b5, b6, bMul,
b1, b2, b3, bSub,
b0, bClr, bEq, bAdd
};
int x = 20, y = 80;
for (int i = 0; i < [Link]; i++) {
buttons[i].setBounds(x, y, 55, 40);
add(buttons[i]);
buttons[i].addActionListener(this);
x += 60;
if ((i + 1) % 4 == 0) {
x = 20;
y += 50;
}
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if ([Link]() == bClr) {
[Link](""); num1 =
num2 = result = 0;
}
if ([Link]() == bEq) {
num2 = [Link]([Link]());
switch (operator) {
_________________________________________________________________________________
______________________________________
v Kasi Janardhan Reddy (92400118862)
J Venkata Praveen (92400118839)
case '+': result = num1 + num2; break;
case '-': result = num1 - num2; break;
case '*': result = num1 * num2; break;
case '/': result = num1 / num2; break;
}
[Link]([Link](result));
}
if ([Link]() == bAdd || [Link]() == bSub ||
[Link]() == bMul || [Link]() == bDiv) {
num1 = [Link]([Link]());
operator = [Link]().charAt(0);
[Link]("");
}
JButton[] nums = { b0, b1, b2, b3, b4, b5, b6, b7, b8, b9 };
for (JButton b : nums) {
if ([Link]() == b) {
[Link]([Link]() + [Link]());
}
}
}
public static void main(String[] args) {
new CalculatorSwing();
}}
______________________________________________________________________________
v kasi Janardhan reddy (92400118862)
v Kasi Janardhan Reddy (92400118862)
J Venkata Praveen (92400118839)
Output:
Swing (Interface)
Result
The Calculator application using Java Swing was successfully designed and executed. The
program correctly performs addition, subtraction, multiplication, and division operations based
on user input and displays accurate results through a graphical interface. Thus, the objective of
implementing a basic calculator using Java Swing and event handling was achieved.
______________________________________________________________________________
v kasi Janardhan reddy (92400118862)
v Kasi Janardhan
J Venkata Reddy
Praveen (92400118862)
(92400118839)