A lightweight desktop calculator built with Java Swing. The project demonstrates graphical user-interface construction, event-driven programming, input parsing, and basic error handling in a compact Java application.
- Addition, subtraction, multiplication, and division
- Button-based input for digits and operators
- Clear (
C) and calculate (=) actions - Support for multi-digit values
- Decimal results through
doublearithmetic - Validation for malformed expressions
- Explicit handling of division by zero
- Swing event handling through a shared
ActionListener
- Java
- Java Swing
- AWT event handling
No third-party dependencies are required.
- JDK 8 or newer
- A desktop environment capable of displaying Swing applications
Clone the repository:
git clone https://github.com/andreiboscu/java-swing-calculator.git
cd java-swing-calculatorCompile the application:
javac -d out Calculator.javaRun it:
java -cp out lab7.CalculatorYou can also import Calculator.java into IntelliJ IDEA, Eclipse, or another Java IDE and run the main method.
Enter an expression using the calculator buttons, then press =.
Example:
123 + 7
The calculator expects one operation at a time in the form:
number operator number
Press C to clear the display. Invalid input produces an error message, while division by zero is reported separately.
.
├── Calculator.java
├── LICENSE
├── README.md
└── .gitignore
Java · Swing · GUI Development · ActionListener · Event-Driven Programming · Input Validation · Exception Handling
- Add decimal-point and backspace buttons
- Support chained calculations
- Improve keyboard input
- Use layout managers for responsive resizing
- Separate calculation logic from the graphical interface
- Add automated unit tests
- Package the application as an executable JAR
This project is available under the MIT License.