Title: Calculator Problem
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

Calculator Problem

Create a Calculator based on JFrame. You can keep the logic for the calculator as simple as possible. You only need to support 2 operands, 1 operator and you need to generate an answer when the "=" button is clicked. So your approach could be:

1. User enters in Operand1
2. When the user clicks on one of the operators (+,-,*,/), your program captures the number in the TextField and stores it as operand1. Your program also stores the operator clicked. Then your program clears the TextField to allow the user to enter in the second operand.
3. User enters in Operand2
4. When the user clicks on the "=" button, your program will compute the desired result from operand1, operand2 and the operator. The answer is then stored into the TextField.

Use a BorderLayout for the overall layout. In the NORTH, put a JTextField. In the CENTER, create a gridLayout for the buttons.

"Calculator" should show up on the Titlebar.

Make your JTextField read only (user can't enter numbers in directly). This avoids getting garbage in the field. The JTextField method to use for this is: setEditable(boolean b)

The font to use for all text should be SERIF, BOLD, 32 points. The colors used in the image below are red, cyan, yellow and lightGray.

Note the lightGray showing through in the gaps of the GridLayout are the background color of the JFrame. The gaps you see are due to using a GridLayout constructor that takes 4 ints. The third and fourth int specify information related to the GridLayout gap information(I used 4 pixels of gap).

Last Updated: March 9, 2015 10:13 AM