Java Calculator Project Explanation
This project is a simple GUI calculator built using Java Swing. It allows users to input two
numbers, perform basic arithmetic operations, and view the result.
1. Imports
The program imports javax.swing.*, java.awt.*, and java.awt.event.* to create the GUI
and handle events like button clicks and key presses.
2. Class Declaration
The class is named Calculus and it extends JFrame to create the main window. It
implements ActionListener and KeyListener to handle interactions.
3. Variables
It defines JTextFields for number inputs and result display, and JButtons for operations
(+, -, *, /, =, Clear). A 'char operator' is used to store the selected operation.
4. Constructor (Calculus)
This sets up the GUI layout, initializes all components, positions them using
setBounds(), and registers action and key listeners.
5. actionPerformed Method
Triggered when buttons are clicked. It sets the operator or triggers result calculation or
clearing of fields.
6. calculateResult Method
Reads values from the text fields, performs the selected operation, and shows the result
in the resultField.
7. clearAll Method
Resets all fields and the operator, preparing the calculator for a new calculation.
8. keyPressed Method
Allows the use of keyboard keys (+, -, *, /, =, Enter, and C) to perform the same actions
as button clicks.
9. main Method
Starts the application by creating an object of the Calculus class, which runs the
constructor.
Summary
This Java application demonstrates a basic calculator using Swing. It supports mouse
and keyboard interaction and includes labels for student details and project information.
The layout is managed manually using setBounds().