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 javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class CalculatorFrontView {
public static void main(String[] args) {
JFrame frame = new JFrame("24k6111 Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 400);
frame.setLayout(new BorderLayout());
JTextField display = new JTextField();
display.setFont(new Font("Arial", Font.BOLD, 24));
display.setEditable(false);
display.setBackground(new Color(0, 51, 102));
display.setForeground(Color.WHITE);
display.setHorizontalAlignment(JTextField.RIGHT);
frame.add(display, BorderLayout.NORTH);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(4, 4, 5, 5));
panel.setBackground(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);
btn.setFont(new Font("Arial", Font.BOLD, 20));
btn.setToolTipText("Button: " + text);
if (text.matches("[0-9]")) {
btn.setBackground(Color.YELLOW);
btn.setForeground(Color.BLACK);
} else {
btn.setBackground(Color.DARK_GRAY);
btn.setForeground(Color.WHITE);
}
btn.addActionListener(e -> {
String btnText = btn.getText();
switch (btnText) {
case "+": case "-": case "*": case "/":
try {
firstNumber[0] = Double.parseDouble(display.getText());
operator[0] = btnText;
input.setLength(0);
display.setText("");
} catch (NumberFormatException ex) {
display.setText("Error");
}
break;
case "=":
try {
double secondNumber = Double.parseDouble(display.getText());
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) {
display.setText("Cannot divide by 0");
return;
} else {
result = firstNumber[0] / secondNumber;
}
break;
}
display.setText(String.valueOf(result));
input.setLength(0);
} catch (Exception ex) {
display.setText("Error");
}
break;
default:
input.append(btnText);
display.setText(input.toString());
break;
}
});
panel.add(btn);
}
frame.add(panel, BorderLayout.CENTER);
frame.getContentPane().setBackground(new Color(0, 51, 102));
frame.setVisible(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: