Natividad Robin O. BSIT-2B
Natividad Robin O. BSIT-2B
BSIT-2B
Create a new project for this by clicking File > New Project from the NetBeans
menu at the top. Select Java > Java Application from the list boxes, and then
click the Next button.
On step 2 of the wizard, type MyCalculator as the project name. At the bottom,
uncheck "Create main class". This is because a main method will be created for
us by NetBeans when we add a form. Step 2 of the wizard should look like this:
Click the Finish button, and NetBeans will create the project, but not much
else. Have a look at the Projects area on the left in NetBeans and you should
see this (If you can't see the Projects area, click Window > Projects from the
NetBeans menu at the top):
Normally, there's a .java file under the Source Packages name. But because we
unchecked the "Create main class" box, there's no java class file there.
1
Natividad Robin O. BSIT-2B
What we'll do is to add a Form to the project. When the form is created, it
will be created in its own java class file.
To add a form, right click the project name in the Projects window. A menu
will appear:
Select New > JFrame Form from the menu. When you do, you should see the
following wizard appear:
2
Natividad Robin O. BSIT-2B
Here, you are being asked for a name for your Class, and a package name. We've
already created the project, and called it MyCalculator. The package name and
class will go into the project. So, for your Class Name type JavaCalculator.
In the blank package text box, type jCalculator. So we're creating a class
called JavaCalculator, which is in the jCalculator package, which is in the
MyCalculator project.
When the wizard from the previous section has finished, it will create a blank
form in the main window:
3
Natividad Robin O. BSIT-2B
The form is a blank, at the moment, and has an orange rectangle surrounding
it. The orange rectangle means that the form is the currently selected object.
Click away and you'll see a blue rectangle instead. This means that the form
is not selected. Click back onto the form to select it, and you'll see the
orange rectangle again.
Note the two buttons at the top, Source and Design. You're in Design at the
moment. Click on Source to see the following code:
4
Natividad Robin O. BSIT-2B
You can expand and contract the plus symbols to reveal and hide code. Notice,
though, that a main method has been created. When the programme starts it's
the main method that will be called. It's creating a new object from our form,
and setting its visible property to true.
But NetBeans generates all this code for you. Otherwise, you'd have to do it
all yourself!
Have a look at the Projects area on the left again. You'll see that a package
and a class file have been added:
5
Natividad Robin O. BSIT-2B
Click back on the Design button at the top. You'll see your blank form again.
To the right, you'll have noticed two areas: a Palette with a lot of controls
in it, and a Properties area. The Palette should look like this:
6
Natividad Robin O. BSIT-2B
(If you can't see them, click Window> Palette and Window > Properties from the
NetBeans menu.)
A property of an object is a list of the things you can do with it, like set
the background colour, set the text, set the font, and lots more. Let's change
the title.
7
Natividad Robin O. BSIT-2B
To see what effect changing the title property has, run your programme in the
usual way. When you run the programme for the first time, NetBeans will
display a dialogue box asking you which main method you want to use:
8
Natividad Robin O. BSIT-2B
You only have one, which is in your JavaCalculator class, so just click OK.
Your form should then appear:
9
Natividad Robin O. BSIT-2B
The form has the title you just typed, plus some default icons for minimize,
maximize and close. Click the X to close your programme, and return to the
Design environment.
What our form needs now is a text box and some buttons. Let's add the text box
first.
10
Natividad Robin O. BSIT-2B
Controls in the NetBeans Palette can be dragged onto a form. So click on Text
Field to select it. Now hold you left mouse button down on the Text Field.
Keep it held down and drag the control onto the form:
11
Natividad Robin O. BSIT-2B
The squares around the text field are sizing handles. You can hold down your
left mouse button on one of the squares and drag to a new width or height. The
dotted lines are position indicators, one for the left position and one for
the top position. Notice that the default name for the text field is
jTextField1. Let's change that.
With the text field selected, have a look at the Navigator area in the bottom
left: (If you can't see an Navigator area, click Window > Navigator from the
NetBeans menu bar.)
As you can see, jTextField1 is selected in the Navigator. This area shows you
what objects you have on your forms. You can also rename an object from here.
To do so, right click on jTextField1. From the menu that appears, select
Change Variable Name.
12
Natividad Robin O. BSIT-2B
When you click on Change Variable Name, a dialogue box appears. Type a new
name for the Text Field. Call it txtDisplay:
Click OK. When you do, NetBeans will rename your text field:
Now have a look at your code again by clicking the Source button in the main
window. When your code appears, scroll down to the bottom. You'll see that a
new private field variable has been added:
13
Natividad Robin O. BSIT-2B
So a JTextField variable has been set up with the name txtDisplay. The
"javax.swing" part is a reference to a set of packages that are used for GUI
development. Swing makes it easier for you to create forms and the controls on
forms.
Click on the Design button at the top to return to your form. The text field
has some default text in, at the moment. You can add your own text by changing
the text property of the text field.
Make sure your text field is selected on the form. Now locate text in the
properties window:
14
Natividad Robin O. BSIT-2B
15
Natividad Robin O. BSIT-2B
Then press the enter key on your keyboard. Have a look at your text field
object on the form. It may well have changed size. Use the sizing handles to
resize it. You'll see that it now has no text in it at all:
16
Natividad Robin O. BSIT-2B
You add a button to a form in the same way you do for text fields - drag and
drop. However, as we're going to be adding lots of buttons, it's a good idea
to add the buttons to a control called a Panel. If you need to move the
buttons, you can then just move the Panel instead. All the buttons will then
move with the Panel.
Drag one on to your form. You can't see a Panel, as they'll have the same
colour as the form. But you can select it:
17
Natividad Robin O. BSIT-2B
Drag the sizing handles so that the Panel fills most of the form:
18
Natividad Robin O. BSIT-2B
19
Natividad Robin O. BSIT-2B
You should then see a button with sizing handles and position lines:
The default name of the button is jButton1. Change this name just like you did
for the text field: right click in the Navigator area, and select Change
variable name. Change the name of the button to btnOne:
20
Natividad Robin O. BSIT-2B
// Variables declaration
Java Instance variables have been set up for the button and panel
The new variable name is btnOne, and it is a JButton object, which is a Swing
control. Note, too, that a variable has been set up for the panel (we've left
this on the default name).
21
Natividad Robin O. BSIT-2B
Go back to Design view and make sure your button is selected. We can change
the text on the button. What we want is a number, one number for each button
on the calculator. The text property is used for button text. So locate this
property in the properties window. Delete the default and type the number 1
instead:
Press the enter key on your keyboard and you should see the text change on
your button. It will also be resized:
22
Natividad Robin O. BSIT-2B
You can change the size of the button in the properties window. Locate
Horizontal Size and Vertical Size, under the Layout heading:
23
Natividad Robin O. BSIT-2B
You can also change the font and font size in the properties window. Make sure
your button is selected. Now locate the font property. Click the small button
to the right of the font row:
24
Natividad Robin O. BSIT-2B
When you click the font button, you should see a dialogue box appear. Change
the font size to 14 points, and make it bold:
Your form should now look like this (to see the outline of the panel, just
hover your mouse over it):
25
Natividad Robin O. BSIT-2B
You now need to add nine more buttons in the same way, for the numbers 2 to 9,
and a 0. Change each variable name to btnTwo, btnThree, btnFour, etc. Delete
the default text, and enter a number instead. Then change the size of each
button to 50, 30. Finally, change the font property of each button.
When you're finished, your Navigator area should look like this:
If your buttons are in the wrong place, click a button to select it. Hold down
your left mouse button. Keep it held down and drag to a new
26
Natividad Robin O. BSIT-2B
location in the panel. To move all the buttons at once, select the panel and
drag it to a new location:
Only three more buttons to add: a plus button, an equals button, and a clear
button. We'll add these to a panel of their own, and move them to the right of
the calculator.
If your form calculator doesn't have any room on the right, you can easily
resize it.
Click on just the form, and not the panel or the text field. If you do it
right, you should see an orange rectangle surrounding the form. (You can
select the JFrame item in the Navigator to select your form.) Now move your
mouse to the edges of the form. The mouse pointer should change shape, as in
the image below (bottom right):
27
Natividad Robin O. BSIT-2B
Hold down your left mouse button when the pointer changes shape. Now drag to a
new size.
Add three buttons to the new panel. Change the variables names to: btnPlus,
btnEquals, and btnClear. For the text for the buttons, type a + symbol for the
28
Natividad Robin O. BSIT-2B
Plus button, a = symbol for equals button, and the word "clear" for the clear
button. Change the font to the same as the number buttons, 14 point bold. The
size for the plus and equals button should be the same as the number buttons:
50, 30. For the Clear button, change it to 70, 30. Your form should then look
like this:
29
Natividad Robin O. BSIT-2B
You can run your form at this stage, just to see what it looks like:
30
Natividad Robin O. BSIT-2B
In Design view in NetBeans, select your number 1 button. Now have a look at
the Navigator window in the bottom left. Locate your btnOne button. Now right
click to see the following menu appear:
Select Event from the menu. From the submenu, click on Action, and then
actionPerformed:
31
Natividad Robin O. BSIT-2B
32
Natividad Robin O. BSIT-2B
textDisplay.setText(btnOneText);
textDisplay.setText(btnTwoText);
textDisplay.setText(btnThreeText);
textDisplay.setText(btnFourText);
textDisplay.setText(btnFiveText);
textDisplay.setText(btnSixText);
textDisplay.setText(btnSevenText);
33
Natividad Robin O. BSIT-2B
textDisplay.setText(btnEightText);
textDisplay.setText(btnNineText);
textDisplay.setText(btnZeroText);
Plus Button
getOperator(button_text);
Minus Button
getOperator(button_text);
Divide Button
getOperator(button_text);
Multiply Button
34
Natividad Robin O. BSIT-2B
getOperator(button_text);
Equals Button
switch ( math_operator ) {
case '+':
break;
case '-':
break;
case '/':
break;
case '*':
break;
txtDisplay.setText( Double.toString(total2) );
total1 = 0;
Clear Button
35
Natividad Robin O. BSIT-2B
total2 = 0;
txtDisplay.setText("");
Public JavaCalculator() {
36