Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)
[GUI] Implement Simple Calculator Using JFrame/Swing In Java | Programs
Latest :

[GUI] Implement Simple Calculator Using JFrame/Swing In Java | Programs

Java program to implement calculator using JFrame/Swing With GUI – In this article, we will detail in on how to implement a calculator using Swing concept in Java programming along with detailed explanation of the source code.

The method used in this article is as follow:

  • Using JFrame

As we all know, calculators are the mechanical or digital instruments which are used generally to carry out basic mathematical calculations using various mathematical operations.

The normally used mathematical operations are as follows:

  • Addition
  • Subtraction
  • Multiplication
  • Division etc.

how to make a calculator in java

As you can see, there are quite a few operations but the above-mentioned ones are the most used mathematical operations.

Simple Calculator Java Source Code – Using JFrame

Output:

java calculator using swing

 

Java Calculator Source Code Explanation

Online calculators are something every single one of us is familiar with. To implement this in Java in JFrame, we have a special package named Java Swing which in short is a toolkit for the graphical user interface (GUI) in Java.

This is nothing but the buttons and view of the calculator we get in order to perform the arithmetic calculations.

JFrame is one such feature that provides us the complete frame to develop our online calculator with components like buttons, labels, textfield, etc. This JFrame class can be inherited in our class and be made us of. Let us split our code and understand it better.

Inside our class (Calculator), we need to first creates all the objects and variables required by us for making our online calculator. Initially, as a part of GUI,

  we will require 10 buttons for the digits 0-9 and also require 6 more buttons for ‘+’, ‘-‘, ‘*’, ‘/’, ‘=’ and ‘C’. So we create JButton objects so all the above required buttons.

So, to perform these operations we create a constructor in class (Calculator) which will contain all these.

But before adding all these buttons to our frame, we need to first specify the layout in which they’ll be placed.

The entire simple calculator is setup within a border layout. This is used to place components in north, south, east, west and center. This is generally the default layout used in a frame.

It is often observed that, we’ll require the use of more than one layout inside a frame for it to be more organized. To implement this, we can make use of JPanel class of the swing package.

It can store group of components and set different layouts for the components. So, we first create an object for the JPanel class followed by which, we’ll make use of GridLayout.

This is used because, as observed in a calculator all buttons are placed in rows and columns in a grid format for all the 16 buttons used.

Now that we have set the layouts, we need to place our components within the frame.

  • So we label the components with names that must be seen by the user (i.e., 0-9 followed by arithmetic operators and ‘C’).
  • As we name each of the component, we add the JButtons to our frame based on the specified grid layout.
  • Since they are all buttons, they must listen or be aware of the action they perform (when button is pressed).

So, for this we’ll also include the addActionListener() method about which we’ll discuss in the later part. All these operations are done as follows:

Apart from all the buttons, we also require a textfield in which the input and output will be displayed.

So we create a JTextField (res) with size mentioned.

This JTextField is outside the JPanel and by using the borderlayout is set to the north of the frame while the entire JPanel (p) where buttons are placed in gridlayout is placed at the center using borderlayout.

We also set the visibility of all of this to be true and set the size of the frame according to our requirement as well.

This marks the end of placing and designing our frame. Now comes the essential part, i.e., when a particular button is pressed what is the action to be performed and what the text field should display, etc.

As we have placed actionListener with buttons, any action performed will be sent to the action performed class.

Any arithmetic operation is between 2 operands (n1 and n2) and an operator (op). So, from buttons we get the information of whether they are pressed or not with the help of getSource method.

Let us consider that, the pb value is equal to b15 (b15 is pressed). b15 is the ‘C’ option which means to clear everything. So, both the operands (n1 and n2) will be set to zero and the textfield (res) is set to empty as follows:

Else, if the value of pb is b14, it means that the ‘=’ option was pressed. Under this condition,

    we have to evaluate our expression with the help of eval() method by taking n2 value from the textfield (res) and convert it to integer since the textfield gives values in string format. with the help of getText method.

The resultant from the eval() will then be displayed in the textfield (res).

If even this is not the case, then we check for the value of pb. If the value is one among b10,b11,b12 0r b13, it means that the operator is pressed.

Therefore, we store the operator value is a variable (op) according to the button clicked and make boolean variable used for finding whether operator is clicked or not (opf) value true.

If the boolean variable (opf) is false it means that one among the 10 buttons from b[0] to b[9] is clicked and it also implies that the button clicked is to store the operand (n1 or n2).

We get the text, and add the button value to the text and set it back to the textfield.

After operator is decided, we retrive this value back from the textfield and store it as our first variable (n1). The second variable (n2) as seen is retrived when ‘=’ operation is clicked before performing eval() method.

When both the operands (n1 and n2) and the operator (op) can received, we perform the eval() method when the ‘=’ button is pressed.

The eval() method consists of switch case based on the operator (op) value that particular operation is performed on the two operands (n1 and n2) and finally the resultant (r) is displayed on the textfield.

Finally, in the main method of Java, the constructor is called anonymously and thereby all the operations mentioned above are executed.

 

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)
x

Check Also

Square Star Pattern Program In Java – Patterns

Java program to print square star pattern program. We have written the below print/draw square ...