Skip to content

Netbeans Calculator Code

Posted on by admin

I'm back and I've got another awesome instructable for you! I'm going to be making a calculator in Java Netbeans, please leave a comment or check out.

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 Sample Output: Sample Jar File: 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. One jTextField. 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:-. 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. 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 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).

Netbeans Calculator Code

Code for jButton1 (The “+” Button). 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(“”);.

Clinical laboratory hematology mckenzie pdf. [download] ebooks clinical laboratory hematology mckenzie pdf pdf your house or the office. You can finely add the soft file to the gadget or every computer unit in. Clinical Laboratory Hematology. Instructor’s Resource Manual in PDF and Word formats. For Clinical Laboratory Hematology, 3rd Edition McKenzie. Clinical Laboratory Hematology continues as a comprehensive. This product is an alternate. Image Library for Clinical Laboratory Hematology, 2nd Edition McKenzie. Browse and Read Clinical Laboratory Hematology Mckenzie Pdf Clinical Laboratory Hematology Mckenzie Pdf It sounds good when knowing the clinical laboratory hematology.

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. Related: Similarly, code for jButton2(The “-” Button). We take another input from the user and store it to global variable num1.


This is done using the statement num1= Float.parseFloat(jTextField1.getText); Then we define another floating point variable res which will store the result. Now using switch-case block we check, which operation is to be performed.

We perform the necessary operation in different cases and store the value to the res variable. The error will occur if the user had not chosen any operation i.e; the op variable will have value ‘$’, so we put an error message in the default section of the switch-case block. There is a possibility of error if you enter the second value as 0 while performing division but the Java infrastructure handles the exception and shows you the result as infinity. Then we display the value in jTextField1. Code for jButton6(The “Clear” Button): This button is used to initialize the variables to zero and operation to ‘$’ i.e; no operation and clears the text field. Simply it clears the memory of the calculator.

After the coding is complete you can save your project by pressing Ctrl+S. Creating the.jar file for the Application You need to follow the following steps to create the.jar file:-. Go to the Run menu - Set Project Configuration - Customise. A Window will open like this:. There click on Browse option of Main Class, and select myCalculator.myCalculator and then click on set as the main class. Then go running menu again and click on Clean and Build Project.

Your “myCalculator.jar” file is created now. Accessing your.jar file To access your myCalculator.jar file you have created, go to the location you have specified while creating the project.

As we have used here the location “D: Codings Netbeans Projects calculator”. On entering this directory you will find a folder named dist.You will get your myCalculator.jar file there. Click on the file to run your project. Congratulations, you have created your first project on Netbeans! If you are a beginner learn If you really liked the article, please subscribe to our for videos related to this article.Please find us on and.

Note: Due to the size or complexity of this submission, the author has submitted it as file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it. Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. FOR YOUR OWN SAFETY, PLEASE: 1. Re-scan downloaded files using your personal virus checker before using it.

NEVER, EVER run compiled files (.exe's,.ocx's,.dll's etc.)-only run source code. Filtered HTML. Web page addresses and e-mail addresses turn into links automatically. You may insert videos with video:URL.

Allowed HTML tags: video. You can enable syntax highlighting of source code with the following tags:,. The supported tag styles are:, foo. Lines and paragraphs break automatically.