Coding the Clear Button of our Java Calculator

Coding the Clear Button of our Java Calculator

 

The only thing left to code for now is the Clear button. For this, we need to clear the total2variable and set the text in the text field to a blank string.

In Design view, double click your Clear button to create the code stub. Now add these two lines to your Clear button:

total2 = 0;
txtDisplay.setText(“”);

Once you’ve added the lines, you can try your calculator out. Run the programme and try adding numbers. You should find that your calculator adds up fine.

 
Exercise
There is one thing missing, however – a point symbol. At the moment, your calculator can’t handle sums like 23.6 + 35.8. So add a point button to your form. Write the code for it. (Hint – it’s practically the same code as for your number buttons.)

 

Exercise
Set the font property for your text field so that it’s 14 point bold
When you’ve completed the above exercises, you calculator should look something like ours:

A completed Java Calculator that can add up

 

Congratulations – you have now written a Java calculator programme! OK, it can only add up, but it’s a start.

What we’ll do now is to make it subtract, divide and multiply.

 

Advertisements

One thought on “Coding the Clear Button of our Java Calculator”

Give A message for us

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s