Random Coding Stuff !

Friday 27 December 2013

NetBeans drag and drop allows the users to work with Graphical User Interface very easily. Today let us create a simple calculator using JAVA and NetBeans.

 For this project I am using NetBeans 7.4. To begin with we need to create a simple project by going to File menu and chose New Project or simply press Ctrl+Shift+N. From the pop up window select Java Application and click Next. In the next Window give your project a name like Calculator and uncheck the last option that is Create a main class. Now on the left hand side, in the Project's pane you will have your project named Calculator and if you browse to the Source Packages, there is a default package. Right Click on that and then New and Add a new JFrame form. In the next Popup give a class name and also set a name for your Package.
                Now you have  a JFrame that has two windows Source and Design. In the design view you can customize the outlook of your program while source view will be used to add functionality to your GUI. First let us design the outlook of our calculator.
On the right hand side you can see a pane named as Palette having different options. In that go to Swing Controls and drag a Label to your JFrame. In the same way drag 3 labels in total. After that you need to drag 3 TextFields to your JFrame. Now finally you need 6 Buttons on your JFrame. After that you need to arrange them according to your needs, Select the first label and right click and then Edit Text or simply press F2 to edit the text. Edit and write Number 1. Same for the label 2 and write Number 2. For the label 3 edit the text and write Result. Now one by one select the all 3 TextFields and delete the text they have. After that select the each Button and edit the text to Add, Sub, Mul, Div, Clear and Exit respectively.
           Now as we are done with the GUI, we need  to add code to our project. Lets begin with the simplest function that is Exit. Right click the Exit Button, Go to Events then Actions and then Action Performed. You will see a window with a code. That code is auto-generated by NetBeans while you work using Drag and Drop. Go to the next line from here your cursor is and write the following line of code.
System.exit(0);


Next is to add code to the clear button. Similarly go to the action Performed and add the code
jTextField1.setText("");

jTextField2.setText("");

jTextField3.setText("");


Now we need to add the code for the sum of two numbers. Right click on the Add button,then Events then Actions and then Action Performed. Add the following code
 float n1, n2, ans;

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

 n2 = Float.parseFloat(jTextField2.getText());

 ans = n1+n2;

 jTextField3.setText(String.valueOf(ans)); 


For subtraction, go to action performed and write
 float n1, n2, ans;

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

 n2 = Float.parseFloat(jTextField2.getText());

 ans = n1-n2;

 jTextField3.setText(String.valueOf(ans)); 


For multiplication, go to action performed and write
 float n1, n2, ans;

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

 n2 = Float.parseFloat(jTextField2.getText());

 ans = n1*n2;

 jTextField3.setText(String.valueOf(ans));


For division go to action performed and write
 float n1, n2, ans;

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

 n2 = Float.parseFloat(jTextField2.getText());

 ans = n1/n2;

 jTextField3.setText(String.valueOf(ans));


This was all about creating a simple calculator. You can modify it by creating buttons for all the numbers and adding additional functions like square root, square etc. 

Pin It

0 comments:

Post a Comment