Java GUI Calculator Report
1. Introduction
This report outlines the creation of a basic calculator using Java's Swing framework. The focus was on
developing a functional graphical user interface (GUI) that supports arithmetic operations. The project
introduced the use of GUI containers, layout managers, and component customization to build a user-
friendly calculator.
2. Java Code
The following Java code implements the GUI calculator:
import [Link].*;
import [Link].*;
import [Link].*;
public class CalculatorFrontView {
public static void main(String[] args) {
JFrame frame = new JFrame("24k6111 Calculator");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](300, 400);
[Link](new BorderLayout());
JTextField display = new JTextField();
[Link](new Font("Arial", [Link], 24));
[Link](false);
[Link](new Color(0, 51, 102));
[Link]([Link]);
[Link]([Link]);
[Link](display, [Link]);
JPanel panel = new JPanel();
[Link](new GridLayout(4, 4, 5, 5));
[Link](new Color(0, 51, 102));
String[] buttons = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+"
};
StringBuilder input = new StringBuilder();
final double[] firstNumber = {0};
final String[] operator = {""};
for (String text : buttons) {
JButton btn = new JButton(text);
[Link](new Font("Arial", [Link], 20));
[Link]("Button: " + text);
if ([Link]("[0-9]")) {
[Link]([Link]);
[Link]([Link]);
} else {
[Link](Color.DARK_GRAY);
[Link]([Link]);
}
[Link](e -> {
String btnText = [Link]();
switch (btnText) {
case "+": case "-": case "*": case "/":
try {
firstNumber[0] = [Link]([Link]());
operator[0] = btnText;
[Link](0);
[Link]("");
} catch (NumberFormatException ex) {
[Link]("Error");
}
break;
case "=":
try {
double secondNumber = [Link]([Link]());
double result = 0;
switch (operator[0]) {
case "+": result = firstNumber[0] + secondNumber; break;
case "-": result = firstNumber[0] - secondNumber; break;
case "*": result = firstNumber[0] * secondNumber; break;
case "/":
if (secondNumber == 0) {
[Link]("Cannot divide by 0");
return;
} else {
result = firstNumber[0] / secondNumber;
}
break;
}
[Link]([Link](result));
[Link](0);
} catch (Exception ex) {
[Link]("Error");
}
break;
default:
[Link](btnText);
[Link]([Link]());
break;
}
});
[Link](btn);
}
[Link](panel, [Link]);
[Link]().setBackground(new Color(0, 51, 102));
[Link](true);
}
}
3. Interface Overview
The calculator features a window containing a display field at the top and a 4x4 grid of buttons below.
Buttons include digits, operations (+, -, *, /), and functional keys such as "." and "=". Color themes are
applied to enhance readability and usability. The deep blue background provides a sleek appearance,
while yellow highlights numeric buttons.
4. Key Takeaways
- Gained practical experience in Java Swing programming
- Learned to design GUI with panels and layout managers
- Applied event handling for interactive buttons
- Practiced styling components using fonts and colors
- Developed a complete, functional GUI calculator
Output: