Sei sulla pagina 1di 9

Creating a Simple Calculator Java

Application using NetBeans


Creating a Simple Calculator Java Application using NetBeans | In this tutorial,
we will learn how to develop a simple calculator application in Java using
Netbeans IDE. We will create a project named myCalculator which contains a
calculator application. This Calculator application is used to compute simple
mathematical operations: Addition, Subtraction, Multiplication and Division of
two numbers and displays the result on the Screen. We will use NetBeans
IDE for developing the project. NetBeans IDE provides effective swing
controls with which designing the app becomes easy. Wants to Learn Learn
complete Java from scratch
Sample Output:

Sample Jar File : [media-downloader media_id=”9487″ texts=”myCalculator”]


Let’s get Started!

Creating a New Project in Netbeans


 Open NetBeans IDE -> click on File Menu -> Click on New Project
 A Window will open, there select Java in categories tab and select Java
Application in Projects tab and then click on Next
 Enter your Project name and location and then click Next. Here we have used
the project name as myCalculator.
 Expand the myCalculator tab in the Palette Window on your left-hand side.
 Again Expand Source Packages and then right click on
the myCalculator tab under Source Packages.
 Select New -> JFrame Form
 A window will open , Enter your Class Name as myCalculator and Click
on Finish
You will get a blank design view opened. Thus your project is created.

Designing the JFrame


We need the following components to design the calculator. These
components are present on the Pallete window in Netbeans IDE on the top
right-hand side of the IDE.

1. One jTextField
2. Six jButtons

You can simply drag and drop the swing controls from the palette window
into the JFrame. Place these components like this :
Here we have done the following modifications:-

1. By default in the jText Field1, it comes jTextField1 written inside the


component. Simply click on the component and use backspace to clear the
text written inside it.
2. We can assign the Text value to the jButton by simply right clicking on the
jButton and select Edit Text. The following jButtons are modified with the
following Texts described below:

 jButton1- ” + ”
 jButton2- ” – “
 jButton3- ” * “
 jButton4- ” / “
 jButton5- ” = “
 jButton6- ” Clear “

Coding Part
Now, Let us come to the coding section of this project/application.
Declaring Global Variables:
Global variables have the scope to any function in java. Here we will declare
three global variables:

static float num=0,num1=0;


static char op='$';

We need two Floating Point (these datatype store integer as well as decimal
values) variables say num and num1, to store data input by the user and to
manipulate them. These variables need to be accessed by every function (i.e;
every button) hence they are declared globally. It is preferred to define such
variables globally rather than declaring the same variable in each and every
function again and again. We initialized the variables with 0 value because if
the user inputs no value then the calculator will take it as 0.
We have another global character variable op. This stores the operation to be
performed ie; ‘+’,’-‘ etc. By default, it stores a character ‘$’ which means no
operation is selected.
To write the code on a component in Netbeans, we double click on the
component. For example, to write code on jButton1 (“+”), we will double click
on jButton1 ( or here on the “+” Button).
Code for jButton1 (The “+” Button):

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

op='+';

num=Float.parseFloat(jTextField1.getText());

jTextField1.setText("");

jTextField1.requestFocusInWindow();

Here First we define the operation by storing ‘+’ character to character


variable op.
Then we store the first number from the user to the global variable num using
the method Float.parseFloat(jTextField1.getText()). Here getText() method
takes the input data from the text field but this data is of string type. So to
explicitly typecast this data as floating point value, we use the method
Float.parseFloat().
Then we erase the jTextField1’s text so as to enable the user to input another
value. This is done using the statement jTextField1.setText(“”); . The
setText() method is used to set a text into the jTextField. If we do not use this
statement then the user has to, again and again, erase the content to enter
another value.
The next line jTextField1.requestFocusInWindow(); means to gain focus of
the cursor to the jTextField after the click of this button. If we do not write this
statement then the user has to place the cursor again and again to the text
field after every operation/click on a button.

Potrebbero piacerti anche