Simple Calculator Program in Java Using AWT | Source Code

Hello Friends, Welcome to my new tutorial about creating a simple calculator program in Java using AWT. In this tutorial, we will see how to design and code the GUI calculator application using Java AWT.

Using this Simple Calculator Application in Java AWT, we will be able to do basic arithmetic operations like addition, subtraction, Multiplication, Division, as well as finding the Square, Square root, and reciprocal of any number.You can download the project’s source code by navigating to the end of this tutorial.

Before starting this tutorial, I assume that you have the basic concept of Java AWT Components and Java Button Click Event, which means what should be the response of a Java Button when we click on it. If you already know about these, it would be a lot easier for you to understand the tutorial.

I will use NetBeans IDE for Developing My Calculator Project, but you can use any IDE like Eclipse, IntelliJ Idea, or simply any text editor like Notepad. It doesn’t matter whatever IDE or text editor you will use for development. The coding and logic will be the same.

So without further ado, let’s start our tutorial Simple calculator program in Java using AWT.

Also Read Simple Calculator Program in Java Using Swing

Simple Calculator Program in Java Using AWT

Creating a New Project in NetBeans

So, first of all, we need to create a new project in NetBeans. Follow these steps to create a new project.

  • Open the NetBeans IDE, click on the File menu, and select New Project.
Simple Calculator Program in Java Using AWT
Simple Calculator Program in Java Using AWT – Fig – 1
  • After you click on the New Project, a new window will be opened where you have to select the Project type as “Java with Ant” and then select the Java Application.
  • Then click on the Next button to proceed.
Simple Calculator Program in Java Using AWT
Simple Calculator Program in Java Using AWT – Fig – 2
  • Again, a new window will be opened where you have to give the name of your project (AwtCalculator in this Example) and tick mark in the “Create Main Class” checkbox.
  • Then click on the Finish button to finish creating the Project.
Simple Calculator Program in Java Using AWT
Simple Calculator Program in Java Using AWT – fig – 3

Creating Display Window/Frame

In this step, we will create a display window, or we can say Frame for our calculator.

  • First of all, import all the necessary packages, which are,
  • We need to import the java.awt.event package too, because we will deal with button click events in our program.
  • Next, Create an object of the Frame class and then set some of its properties like its size, location, layout, Background color, Visibility, and the close operation using the methods of the Frame class.
  • Now Run your program.
Simple Calculator Program in Java Using AWT
Simple Calculator Program in Java Using AWT – Fig – 4
  • As you can see, we have successfully created our display window, and the next thing we have to do is add components to it.

Adding Components to Frame/Window

In this step, we will create the object of all the components that we want to add to our Frame/Window, which are,

    • Buttons from 0 to 9.
    • One clear Button and a delete button.
    • Buttons for arithmetic operations.
    • One button for negative Calculations.
    • A text field for displaying the result.
    • Two checkboxes for switching between on and off.
    • One label for displaying the calculation.
  • After creating the object of all the components, we will set some of the properties of each component, like setting their size, location, and some other properties.
  • After setting their properties, we will finally add them to the frame(Window) using the add() method.
  • Now run your program.
Simple Calculator program in Java using AWT
Simple Calculator program in Java using AWT – Fig – 5
  • As you can see that we have successfully added the components to our Frame/Window.
  • Now we have to add functionalities to our button so that some action should be performed when we click on a particular button.
    • For this, we will implement the ActionListener interface in our class. If we implement the ActionListener interface in our class, we have to override its method actionPerformed() in that class.
    • We have also created the checkboxes for switching between on and off. For this, we need to implement the ItemListener interface in our class. If we implement the ItemListener interface in our class, we have to override its method itemStateChanged() in that class.

Adding Functionalities

  • First of all, we will implement the ActionListener and ItemListener interface in our class, and then we will override their methods actionPerformed() and itemStateChanged() in that class.
  • Next, we will register ActionListener for all the buttons and ItemListener for checkboxes.
  • Next, all the actions that we want in response when a button is clicked will be coded inside the actionPerformed() method, and when a checkbox is clicked, its code will be coded inside the itemStateChanged() method.
  • Now let’s run our program and do some calculations.
Simple Calculator Program in Java using AWT – Fig – 6

 

Simple Calculator Program in Java Using AWT – Fig – 7
  • As you can see that as soon as we clicked on the multiplication(X) button, the text field got cleared, and the Label showed up at the top of the calculator.
  • Now we need to enter the second number for calculation.
Simple Calculator Program in Java Using AWT – Fig – 8
  • Next, Click on the Equals(=) button to get the Result.
Simple Calculator Program in Java Using AWT – Fig – 9
  • Let’s see what happens when we click on the off button.
Simple Calculator Program in Java Using AWT – Fig – 10
  • As you can see, as soon as we clicked on the “off” button, all the buttons got disabled as well as the text field and label cleared, Except the “on” button so that we can again change it to on state.

Simple Calculator Program in Java Using AWT Source Code

  • You can download the source code of this Calculator project in Java using AWT by clicking on the below link.

So guys, this was all from this tutorial, Simple Calculator Program in Java Using AWT. If you have any questions regarding this post, then you can ask the questions in the comment section.

People are also Reading…..

Leave a Comment