Calculator Program in Java Swing/JFrame with Source Code

Hello friends, today we will learn how we can create a Simple Calculator Program in Java Using Swing with Source Code. This is going to be our first application using swing programming, where we will develop a simple calculator using Swing. This is going to be a simple Java GUI calculator project which will perform basic arithmetic operations like addition, subtraction, multiplication, division, etc. It can also be used for finding the square, square root, and reciprocal of any number.

Before starting this tutorial, I assume that you have the basic concept of swing components, and also you should have the idea of JButton Click Event, which means the response of a button when we click on it. If you already know about the Basics, then we can move ahead to create a calculator using swing components in Java.

I will use NetBeans IDE(integrated development environment) for the coding part of my calculator program. You can use any IDE like IntelliJ IDEA, Eclipse or you can just use notepad++ to code your program. It doesn’t matter whatever IDE or editor you will use the programming logic will be the same.

So friends, lets get started with our tutorial Simple Calculator Program in Java Using Swing or can say simple calculator program in Java using JFrame/frame step by step. You can also download the Java Calculator Source Code at the end of this tutorial.

Related Articles
Simple Calculator Program in Java Using AWT
Login Form in Java Swing

Simple Calculator Program in Java Using Swing

Creating a New Calculator Project in NetBeans

Step 1

First of all, we need to create a new project in Netbeans. For this, follow these steps.

  • Open the NetBeans IDE and then click on the File menu and then select New Project.
Simple Calculator Program in Java Using Swing
Calculator Program in Java Swing – fig-1
  • After that, a window will be opened. From there, select Java in categories and Java Application in projects and then click on the Next Button.
Simple Calculator Program in Java Using Swing
Calculator Program in Java Swing – fig – 2
  • After you click on the Next button again a new window will be opened and there you have to give the name of the Project (Calculator in this Example) and also uncheck the create Main class option.
  • Then click on the Finish Button.
Simple Calculator Program in Java Using Swing
Calculator Program in Java Swing – fig – 3
  • Now expand your project using the + icon and right-click on the source packages.
  • Then select new>>Java class.
Simple Calculator Program in Java Using Swing
Calculator Program in Java Swing – fig – 4
  • A new window will be opened where you have to give the name of your Java class (Calculator in this Example).
  • Then click on the Finish button.
Calculator Program in Java Swing – fig – 5

Creating Display Window for Calculator

Step 2

Now we will create a display window or frame for our calculator.

  • Create an object of the JFrame class and set some of its properties like its title, visibility, background color, layout property etc. using the method of the JFrame class.
  • Now create another Java class(MainClass in this example) and inside the main method just create an object of our Calculator class.
  • To create New Java class, right-click on the source packages and then select New >> Java Class.
Calculator Progarm in Java Swing – fig – 6
  • A new window will be opened where you have to give the name of your Java class (MainClass in this Example).
  • Then click on the Finish button.
Calculator Progarm in Java Swing – fig – 7
  • After that, create the main method and create an object of the Calculator class inside the main method.
  • After that, we need to run or program. To run our program right click on the file MainClass.java and then select Run file.
Calculator Progarm in Java Swing – fig – 8

After Running the Program, you will see the output below.

Simple Calculator Program in Java Using Swing
Simple Calculator Program in Java Using Swing-fig-9
  • Now as you can see that we have successfully created our display.
  • Now the next thing we have to do is to add components to it.

Adding Components to Our Display Window/Designing Calculator

Step 3

  • In this step, we will design our calculator.
  • For this, we will create object of all the components that we want to add to our window like,
    • Buttons from 0 to 9.
    • One clear Button and a delete button.
    • Buttons for arithmetic operations.
    • A text field for displaying the result.
    • Two radio buttons for switching between on and off.
    • One label for displaying the calculation.
  • After creating object of all the components, we will set some of the properties of each component like setting their size, location and some other properties and 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 Swing
Simple Calculator Program in Java Using Swing-fig-10
  • As we can see that we have successfully added components to our window.
  • Now we have to add functionalities to our button so that when we click on a particular button, some action should be performed.
  • For this, we will implement ActionListener interface to our class, and if we are implementing ActionListener interface in our class then we have to override its method actionPerformed() in that class.

Adding functionalities to buttons

step 4

  • For this first of all we will implement the ActionListener interface in our class and then we will override its method actionPerformed() in that class.
  • Next, we will register ActionListener for all the buttons.
  • Next, all the actions that we want in response when a button is clicked will be coded inside actionPerformed() method in that class.
  • So finally, we have completed to code our GUI Calculator in Java using Swing components. Now Let’s run our program and do some calculations.
Simple Calculator Program in Java Using Swing
Simple Calculator Program in Java Using Swing-fig-11
Simple Calculator Program in Java Using Swing-fig-12
  • You can see that as soon as we have clicked on the plus button, the text field is cleared and label showed up at the top of our calculator.
  • Now we can enter the second number for our calculation.
Simple Calculator Program in Java Using Swing-fig-13
  • Now we have to click on the equals(=) button to get our result.
Simple Calculator Program in Java Using Swing-fig-14
  • Now let’s click on the off button and see what happens.
Simple Calculator Program in Java Using Swing-fig-15
  • As you can see it disabled all the buttons and cleared the label and the text field.
  • You can also download this Java Calculator Source Code from the download button given below.

Simple Calculator Program in Java Source Code

  • Here you can download the Java Calculator Source Code.
  • The Link of the file is given below.

Calculator Program in Java using Swing PDF Download

  • Her you can download the PDF of Calculator Program in Java using Swing.
  • The Link of the file is given below.

So guys, this was all from this post in which we learned how to create a calculator program in Java swing Using the NetBeans IDE. If you have any doubts about this NetBeans Java Calculator Project, feel free to ask the question in the comments section.

People Are Also Reading…

7 thoughts on “Calculator Program in Java Swing/JFrame with Source Code”

      • Please do it for me. Because I really want to be a good Programmer. I love your style of coding and solving problems.

        I will appreciate every effort you will apply for me to become a good Java Programmer.
        Thank you so much for such tutorial.

        Reply

Leave a Comment