6
6
import java .util .regex .Matcher ;
7
7
import java .util .regex .Pattern ;
8
8
9
- /**
9
+ /**=====================================================================================================================
10
10
* User: chris
11
11
* Date: 3/8/13
12
12
* Time: 4:01 PM
13
13
* CIS 611 Assignment #5
14
14
*
15
15
* The GUI for the calculator application
16
- */
16
+ ===================================================================================================================== */
17
17
18
- public class Application implements ActionListener {
18
+ public class Application extends JFrame implements ActionListener {
19
19
20
20
public static final String BUTTON_TEXT_ADD = "Add" ;
21
21
public static final String BUTTON_TEXT_SUBTRACT = "Subtract" ;
22
22
public static final String BUTTON_TEXT_MULTIPLY = "Multiply" ;
23
23
public static final String BUTTON_TEXT_DIVIDE = "Divide" ;
24
- JTextField resultsField ;
25
- JTextField num1Field ;
26
- JTextField num2Field ;
24
+ private JTextField resultsField ;
25
+ private JTextField num1Field ;
26
+ private JTextField num2Field ;
27
27
private JLabel num1Label ;
28
28
private JLabel num2Label ;
29
29
private JLabel resultsLabel ;
30
30
31
- // the main method for setting up the GUI
31
+ //==================================================================================================================
32
+ // the constructor is the main method for setting up the GUI
32
33
public Application () {
33
34
34
- JFrame frame = new JFrame ("My Calculator" );
35
-
36
- frame .setSize (550 ,100 );
37
- frame .setLocationRelativeTo (null );
38
- frame .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
39
-
40
-
41
- doTheLayout (frame );
42
- frame .pack ();
43
- frame .setVisible (true );
35
+ // call our method to create and add all the controls to the window
36
+ makeTheObjects ();
37
+ doTheLayout (this );
44
38
45
39
}
46
40
41
+ //==================================================================================================================
47
42
private void doTheLayout (JFrame frame ) {
43
+ // create a panel with the buttons on it the panels allow me to
44
+ // layout the controls at a more granular level add the panel to the frame
48
45
JPanel buttonsPanel = getButtonsPanel ();
46
+
47
+ // create a panel with the fields on it
49
48
JPanel fieldsPanel = getFieldsPanel ();
49
+
50
+ // this layout lets you put components around the outside of the frame
50
51
BorderLayout calculatorLayout = new BorderLayout ();
51
52
frame .setLayout (calculatorLayout );
52
53
54
+ // the north position is just at the top of the layout
53
55
frame .add (fieldsPanel , BorderLayout .NORTH );
56
+ // center puts the panel in the frame and allows it to expand to fit the available space
54
57
frame .add (buttonsPanel , BorderLayout .CENTER );
55
58
}
56
59
60
+ //==================================================================================================================
57
61
private JPanel getFieldsPanel () {
58
62
JPanel fieldsPanel = new JPanel ();
59
- FlowLayout gridLayout = new FlowLayout ();
60
- fieldsPanel .setLayout (gridLayout );
61
- makeTheObjects ();
63
+ // create a layout for the fields that will just place them left to right
64
+ FlowLayout flowLayout = new FlowLayout ();
65
+ fieldsPanel .setLayout (flowLayout );
66
+
62
67
68
+ // add the fields that we created to the panel in the correct order
63
69
fieldsPanel .add (num1Label );
64
70
fieldsPanel .add (num1Field );
65
71
fieldsPanel .add (num2Label );
66
72
fieldsPanel .add (num2Field );
67
73
fieldsPanel .add (resultsLabel );
68
74
fieldsPanel .add (resultsField );
69
75
70
-
71
76
return fieldsPanel ;
72
77
}
73
78
79
+ //==================================================================================================================
80
+ // create the form controls
74
81
private void makeTheObjects () {
82
+ // this method creates the labels and the text fields for the input
75
83
num1Label = new JLabel ("Number 1:" );
76
84
num2Label = new JLabel ("Number 2:" );
77
85
resultsLabel = new JLabel ("Results:" );
86
+ // create and set the sizes of the text fields
78
87
num1Field = new JTextField (5 );
79
88
num2Field = new JTextField (5 );
89
+ // create the results field and make it read only
80
90
resultsField = new JTextField (15 );
81
91
resultsField .setEditable (false );
82
92
}
83
93
84
-
94
+ //==================================================================================================================
85
95
// create the buttons for the calculator
86
96
private JPanel getButtonsPanel () {
87
97
JPanel numbersPanel = new JPanel ();
98
+
99
+ // make a layout that is one component high and four wide
88
100
GridLayout gridLayout = new GridLayout (1 , 4 );
89
101
numbersPanel .setLayout (gridLayout );
90
102
91
-
103
+ // put the buttons in the panel in the order that they should
104
+ // be displayed left to right
92
105
addCalculatorButton (numbersPanel , BUTTON_TEXT_ADD );
93
106
addCalculatorButton (numbersPanel , BUTTON_TEXT_SUBTRACT );
94
107
addCalculatorButton (numbersPanel , BUTTON_TEXT_MULTIPLY );
@@ -97,20 +110,32 @@ private JPanel getButtonsPanel() {
97
110
98
111
}
99
112
113
+ //==================================================================================================================
114
+ // create a button and set up the action listener for it
100
115
private void addCalculatorButton (JPanel numbersPanel , String buttonText ) {
101
116
JButton button = new JButton (buttonText );
117
+ // make the first letter of the caption a keyboard shortcut
102
118
button .setMnemonic (buttonText .charAt (0 ));
119
+ // the Application class implements ActionListener so add this as
120
+ // the listener
103
121
button .addActionListener (this );
104
122
numbersPanel .add (button );
105
123
}
106
124
125
+ //==================================================================================================================
126
+ // the action listener will be called when an action on a control that is associated
127
+ // with the listener takes place
107
128
@ Override
108
129
public void actionPerformed (ActionEvent actionEvent ) {
109
130
131
+ // make sure that the user typed in a valid number for our purposes
110
132
if (validateNumberFields ()) {
133
+
134
+ // parse the string that's in the text field to a double precision floating point number
111
135
double number1 = Double .parseDouble (num1Field .getText ());
112
136
double number2 = Double .parseDouble (num2Field .getText ());
113
137
138
+ // do the correct operation and display the results in the appropriate field
114
139
if (actionEvent .getActionCommand ().equals (BUTTON_TEXT_ADD )) {
115
140
resultsField .setText (String .valueOf (number1 + number2 ));
116
141
@@ -121,28 +146,36 @@ public void actionPerformed(ActionEvent actionEvent) {
121
146
resultsField .setText (String .valueOf (number1 * number2 ));
122
147
123
148
} else if (actionEvent .getActionCommand ().equals (BUTTON_TEXT_DIVIDE )) {
149
+ // we definitely don't want to divide by zero!
124
150
if (number2 != 0 ) {
125
151
resultsField .setText (String .valueOf (number1 / number2 ));
126
152
} else {
127
- JOptionPane .showMessageDialog (null , "Number 2 cannot be zero if you are dividing." , "Error" , JOptionPane .ERROR_MESSAGE );
153
+ JOptionPane .showMessageDialog (null , "Number 2 cannot be zero if you are dividing." , "Error"
154
+ , JOptionPane .ERROR_MESSAGE );
128
155
129
156
}
130
157
131
158
}
132
159
}
133
-
134
160
}
135
161
162
+ //==================================================================================================================
163
+ // use a regular expression to validate that the number is in the correct format.
164
+ // display an error message if it is not
136
165
private boolean validateNumberFields () {
137
- Pattern regexPattern = Pattern .compile ("[0-9.]+" );
166
+
167
+
168
+ Pattern regexPattern = Pattern .compile ("[\\ -0-9.]+" );
138
169
Matcher matcher = regexPattern .matcher (num1Field .getText ());
139
170
if (! matcher .matches ()) {
140
- JOptionPane .showMessageDialog (null , "Number 1 must be a whole or decimal number. Do not include any other symbols or punctuation " , "Error" , JOptionPane .ERROR_MESSAGE );
171
+ JOptionPane .showMessageDialog (null , "Number 1 must be a whole or decimal number. Do not include" +
172
+ " any other symbols or punctuation " , "Error" , JOptionPane .ERROR_MESSAGE );
141
173
return false ;
142
174
}
143
175
matcher = regexPattern .matcher (num2Field .getText ());
144
176
if (! matcher .matches ()) {
145
- JOptionPane .showMessageDialog (null , "Number 2 must be a whole or decimal number. Do not include any other symbols or punctuation" , "Error" , JOptionPane .ERROR_MESSAGE );
177
+ JOptionPane .showMessageDialog (null , "Number 2 must be a whole or decimal number. Do not include" +
178
+ " any other symbols or punctuation" , "Error" , JOptionPane .ERROR_MESSAGE );
146
179
return false ;
147
180
}
148
181
return true ;
0 commit comments