Skip to content

Commit d35084c

Browse files
committed
finished
1 parent adc971e commit d35084c

File tree

2 files changed

+77
-30
lines changed

2 files changed

+77
-30
lines changed

src/Application.java

+61-28
Original file line numberDiff line numberDiff line change
@@ -6,89 +6,102 @@
66
import java.util.regex.Matcher;
77
import java.util.regex.Pattern;
88

9-
/**
9+
/**=====================================================================================================================
1010
* User: chris
1111
* Date: 3/8/13
1212
* Time: 4:01 PM
1313
* CIS 611 Assignment #5
1414
*
1515
* The GUI for the calculator application
16-
*/
16+
=====================================================================================================================*/
1717

18-
public class Application implements ActionListener {
18+
public class Application extends JFrame implements ActionListener {
1919

2020
public static final String BUTTON_TEXT_ADD = "Add";
2121
public static final String BUTTON_TEXT_SUBTRACT = "Subtract";
2222
public static final String BUTTON_TEXT_MULTIPLY = "Multiply";
2323
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;
2727
private JLabel num1Label;
2828
private JLabel num2Label;
2929
private JLabel resultsLabel;
3030

31-
// the main method for setting up the GUI
31+
//==================================================================================================================
32+
// the constructor is the main method for setting up the GUI
3233
public Application() {
3334

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);
4438

4539
}
4640

41+
//==================================================================================================================
4742
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
4845
JPanel buttonsPanel = getButtonsPanel();
46+
47+
// create a panel with the fields on it
4948
JPanel fieldsPanel = getFieldsPanel();
49+
50+
// this layout lets you put components around the outside of the frame
5051
BorderLayout calculatorLayout = new BorderLayout();
5152
frame.setLayout(calculatorLayout);
5253

54+
// the north position is just at the top of the layout
5355
frame.add(fieldsPanel, BorderLayout.NORTH);
56+
// center puts the panel in the frame and allows it to expand to fit the available space
5457
frame.add(buttonsPanel, BorderLayout.CENTER);
5558
}
5659

60+
//==================================================================================================================
5761
private JPanel getFieldsPanel() {
5862
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+
6267

68+
// add the fields that we created to the panel in the correct order
6369
fieldsPanel.add(num1Label);
6470
fieldsPanel.add(num1Field);
6571
fieldsPanel.add(num2Label);
6672
fieldsPanel.add(num2Field);
6773
fieldsPanel.add(resultsLabel);
6874
fieldsPanel.add(resultsField);
6975

70-
7176
return fieldsPanel;
7277
}
7378

79+
//==================================================================================================================
80+
// create the form controls
7481
private void makeTheObjects() {
82+
// this method creates the labels and the text fields for the input
7583
num1Label = new JLabel("Number 1:");
7684
num2Label = new JLabel("Number 2:");
7785
resultsLabel = new JLabel("Results:");
86+
// create and set the sizes of the text fields
7887
num1Field = new JTextField(5);
7988
num2Field = new JTextField(5);
89+
// create the results field and make it read only
8090
resultsField = new JTextField(15);
8191
resultsField.setEditable(false);
8292
}
8393

84-
94+
//==================================================================================================================
8595
// create the buttons for the calculator
8696
private JPanel getButtonsPanel() {
8797
JPanel numbersPanel = new JPanel();
98+
99+
// make a layout that is one component high and four wide
88100
GridLayout gridLayout = new GridLayout(1, 4);
89101
numbersPanel.setLayout(gridLayout);
90102

91-
103+
// put the buttons in the panel in the order that they should
104+
// be displayed left to right
92105
addCalculatorButton(numbersPanel, BUTTON_TEXT_ADD);
93106
addCalculatorButton(numbersPanel, BUTTON_TEXT_SUBTRACT);
94107
addCalculatorButton(numbersPanel, BUTTON_TEXT_MULTIPLY);
@@ -97,20 +110,32 @@ private JPanel getButtonsPanel() {
97110

98111
}
99112

113+
//==================================================================================================================
114+
// create a button and set up the action listener for it
100115
private void addCalculatorButton(JPanel numbersPanel, String buttonText) {
101116
JButton button = new JButton(buttonText);
117+
// make the first letter of the caption a keyboard shortcut
102118
button.setMnemonic(buttonText.charAt(0));
119+
// the Application class implements ActionListener so add this as
120+
// the listener
103121
button.addActionListener(this);
104122
numbersPanel.add(button);
105123
}
106124

125+
//==================================================================================================================
126+
// the action listener will be called when an action on a control that is associated
127+
// with the listener takes place
107128
@Override
108129
public void actionPerformed(ActionEvent actionEvent) {
109130

131+
// make sure that the user typed in a valid number for our purposes
110132
if (validateNumberFields()) {
133+
134+
// parse the string that's in the text field to a double precision floating point number
111135
double number1 = Double.parseDouble(num1Field.getText());
112136
double number2 = Double.parseDouble(num2Field.getText());
113137

138+
// do the correct operation and display the results in the appropriate field
114139
if(actionEvent.getActionCommand().equals(BUTTON_TEXT_ADD)) {
115140
resultsField.setText(String.valueOf(number1 + number2));
116141

@@ -121,28 +146,36 @@ public void actionPerformed(ActionEvent actionEvent) {
121146
resultsField.setText(String.valueOf(number1 * number2));
122147

123148
} else if(actionEvent.getActionCommand().equals(BUTTON_TEXT_DIVIDE)) {
149+
// we definitely don't want to divide by zero!
124150
if(number2 != 0) {
125151
resultsField.setText(String.valueOf(number1 / number2));
126152
} 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);
128155

129156
}
130157

131158
}
132159
}
133-
134160
}
135161

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
136165
private boolean validateNumberFields() {
137-
Pattern regexPattern = Pattern.compile("[0-9.]+");
166+
167+
168+
Pattern regexPattern = Pattern.compile("[\\-0-9.]+");
138169
Matcher matcher = regexPattern.matcher(num1Field.getText());
139170
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);
141173
return false;
142174
}
143175
matcher = regexPattern.matcher(num2Field.getText());
144176
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);
146179
return false;
147180
}
148181
return true;

src/Calculator.java

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import javax.swing.*;
2+
13
/**
24
* User: chris
35
* Date: 3/8/13
@@ -9,10 +11,22 @@
911
public class Calculator {
1012

1113
public static void main(String[] args) {
12-
Application application = new Application();
1314

14-
}
15+
JFrame f = new Application();
16+
// set the desired size for the frame
17+
f.setSize(550,120);
18+
19+
// set the window in relation to nothing - center it
20+
f.setLocationRelativeTo(null);
1521

22+
// what to do with the application when this frame is closed - exit the application
23+
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
1624

25+
f.setTitle("My Calculator");
1726

27+
// size the frame so that all the components are the desired size
28+
f.pack();
29+
30+
f.setVisible(true);
31+
}
1832
}

0 commit comments

Comments
 (0)