You are on page 1of 4

Creation of the Project and Frame Step 1: Open Java NetBeans IDE 6.5.

1 through Start , programs , NetBeans , NetBeans IDE 6.5.1 or by clicking at its shortcut icon on the desktop. Step 2: Click File ? New Project command. Alternately, you can click the New Project Icon in the IDE toolbar or press Ctrl+Shift+N. Step 3: In the Categories pane, select the Java node and in the Projects pane, choose Java Application. Click Next. Step 4: Enter the Project Name as IP Project. Uncheck the first two options and select Set as Main Project option. Finally, click Finish button. Step 5: Next add a frame window to the project where desired components and functionality can be added. For this, on the top left pane, under projects tab, right click on IP Project and select New from the appeared submenu, select JFrame form. Step 6: In the next dialog, specify the name of the new frame being added in the box next to Class Name as Calculator and click Finish. The newly added frame is the top level container of the application. Step 7: In the properties window present on Right most side of the screen, set the title property as Calculator. Adding Controls and Setting Properties Step 1: Drag a panel from the palette, present on the right side of the screen. Set its size as [229,234] in preferred size option of the properties window of the panel by clicking the ellipsis (a small square) present in the option. Enter width as 229 and height as 234. Step 2: Drag a Text Field from the palette inside to the panel, Set its size as [4, 25] in preferred size option of the properties window of the panel by clicking the ellipsis (a small square) present in the option. Enter width as 4 and height as 25. Set its font as Courier New, Font Style as Bold and Size as 18. Uncheck the option editable. Erase the text present in the Text option. In the border option, click the ellipsis and select Etched Border and click ok button. Right click the Text Field, select Change Variable Name option, enter the new name as display in the dialog box and click ok button. Step 3: Add 20 buttons to the frame and arrange it as a calculator inside the panel below the text Field. Step 4: Change the text property and variable names of buttons as following: Button name Variable Name Text Property Button1 Button1 1 Button2 Button2 2 Button3 Button3 3 Button4 Button4 4 Button5 Button5 5 Button6 Button6 6 Button7 Button7 7 Button8 Button8 8 Button9 Button9 9

Button10 Button11 Button12 Button13 Button14 Button15 Button16 Button17 Button18 Button19 Button20

Button0 0 clear C exit Exit decimal . percent % plus + minus multiply X divide / sign +/equals =

Writing Code for the Program Step 1 Click on the source tab present next to the design tab just above the design area. Now a code editor window will get opened. Take the cursor to the line were a similar code is written as below: public class NewJFrame extends javax.swing.JFrame { Click after flower bracket ({) and press enter key in the key board. The cursor will be dropped down to the next line. Now type the following code: //variables double firstDouble; double secondDouble; double totalDouble; //to check for button clicks int plusClick; int minusClick; int multiplyClick; int divideClick; int decimalClick; int powerClick; Step 2: Click on the design tab. The design window gets reopened. Now double click on each button, and then code editor window for that will get opened. Erase the line // TODO add your handling code here: Each time. Write the code as below: For button Button1: display.setText(display.getText()+"1"); For button Button2: display.setText(display.getText()+"2"); For button Button3: display.setText(display.getText()+"3"); For button Button4: display.setText(display.getText()+"4"); For button Button5: display.setText(display.getText()+"5"); For button Button6: display.setText(display.getText()+"6");

For button Button7: display.setText(display.getText()+"7"); For button Button8: display.setText(display.getText()+"8"); For button Button9: display.setText(display.getText()+"9"); For button Button0: display.setText(display.getText()+"0"); For button clear: display.setText(""); decimalClick=1; For button exit: System.exit(0); For button decimal: if (decimalClick==1) { display.setText(display.getText()+"."); decimalClick=0; For button percent: double per; double sec=Double.parseDouble(display.getText()); display.setText(""); per=(firstDouble*100)/sec; display.setText(""+per); For button sign: Double x = Double.parseDouble( display.getText()); x=x*(-1); display.setText(""+x); For button plus: firstDouble=Double.parseDouble(display.getText()); display.setText(""); plusClick=1; decimalClick=1; For button minus: firstDouble=Double.parseDouble(display.getText()); display.setText(""); minusClick=1; decimalClick=1; For button multiply: firstDouble=Double.parseDouble(display.getText()); display.setText(""); multiplyClick=1; decimalClick=1; For button divide: firstDouble=Double.parseDouble(display.getText()); display.setText(""); divideClick=1; decimalClick=1; For button equals: secondDouble=Double.parseDouble(display.getText()); if (plusClick==1)

{ totalDouble=firstDouble+secondDouble; display.setText(""+totalDouble); firstDouble=0; secondDouble=0; plusClick=0; } if (minusClick==1) { totalDouble=firstDouble-secondDouble; display.setText(""+totalDouble); firstDouble=0; secondDouble=0; minusClick=0; } if (multiplyClick==1) { totalDouble=firstDouble*secondDouble; display.setText(""+totalDouble); firstDouble=1; secondDouble=1; multiplyClick=0; } if (divideClick==1) { totalDouble=firstDouble/secondDouble; display.setText(""+totalDouble); firstDouble=1; secondDouble=1; divideClick=0; } Building and Running the Program Step 1: Move the mouse point over the Run option in the menu bar. Now a drop down menu appears, click on the Clean and Build Project option. After the process is completed (within 1 or 2 minutes) again move the mouse point over the Run option in the menu bar, in drop down menu click on the Run Project option. Now an application named Calculator is seen on the screen. Check by doing calculations on it. Isnt it similar to a manual calculator?

You might also like