-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalcStartNew.java
267 lines (236 loc) · 8.09 KB
/
CalcStartNew.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
*code by Reed Dahle
*last updated 01-31-2019 @ 21:44
*My dad works at oracle so if you steal my code without attributing me I can have you banned kid
*/
package calcUI;
import java.awt.EventQueue; //stupid window builder add in won't let me remove useless imports
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.BoxLayout;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.FlowLayout;
import javax.swing.JSplitPane;
import java.awt.Component;
import javax.swing.Box;
import java.awt.Dimension;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
import javax.script.*;
public class CalcStartNew{ //also don't ask why its called CalcStartNew
private JFrame frame; //create all class level evaluators
CalcEval doEval = new CalcEval(); //final evaluator
ExponentEvaluator evalExp = new ExponentEvaluator(); //exponent evaluator ( actually just replaces ^ with Math.pow() )
String evalString = ""; //string taken from textArea and put into evaluators
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CalcStartNew window = new CalcStartNew();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public CalcStartNew(){
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize(){
frame = new JFrame(); //all of this code should be pretty self explanatory; just adds a character to textArea when its button is pressed
frame.setTitle("Calculator");
frame.setBounds(100, 100, 900, 450);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JTextArea textArea = new JTextArea();
textArea.setBounds(12, 12, 876, 138);
frame.getContentPane().add(textArea);
JPanel panel = new JPanel();
panel.setBounds(12, 162, 175, 250);
frame.getContentPane().add(panel);
panel.setLayout(new GridLayout(0, 3, 0, 0));
JButton button = new JButton("1");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.setText(textArea.getText()+"1");
}
});
panel.add(button);
JButton button_1 = new JButton("2");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.setText(textArea.getText()+"2");
}
});
panel.add(button_1);
JButton button_2 = new JButton("3");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.setText(textArea.getText()+"3");
}
});
panel.add(button_2);
JButton button_3 = new JButton("4");
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.setText(textArea.getText()+"4");
}
});
panel.add(button_3);
JButton button_4 = new JButton("5");
button_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.setText(textArea.getText()+"5");
}
});
panel.add(button_4);
JButton button_5 = new JButton("6");
button_5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.setText(textArea.getText()+"6");
}
});
panel.add(button_5);
JButton button_6 = new JButton("7");
button_6.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.setText(textArea.getText()+"7");
}
});
panel.add(button_6);
JButton button_7 = new JButton("8");
button_7.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.setText(textArea.getText()+"8");
}
});
panel.add(button_7);
JButton button_8 = new JButton("9");
button_8.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.setText(textArea.getText()+"9");
}
});
panel.add(button_8);
JButton button_9 = new JButton("0");
button_9.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.setText(textArea.getText()+"0");
}
});
panel.add(button_9);
JButton button_10 = new JButton(".");
button_10.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.setText(textArea.getText()+".");
}
});
panel.add(button_10);
JPanel panel_1 = new JPanel();
panel_1.setBounds(199, 162, 200, 246);
frame.getContentPane().add(panel_1);
panel_1.setLayout(new GridLayout(0, 3, 0, 0));
JButton button_11 = new JButton("+");
button_11.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.setText(textArea.getText()+"+");
}
});
panel_1.add(button_11);
JButton button_12 = new JButton("-");
button_12.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.setText(textArea.getText()+"-");
}
});
panel_1.add(button_12);
JButton button_13 = new JButton("*");
button_13.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.setText(textArea.getText()+"*");
}
});
panel_1.add(button_13);
JButton btnC = new JButton("/");
btnC.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.setText(textArea.getText()+"/");
}
});
panel_1.add(btnC);
JButton button_15 = new JButton("(");
button_15.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.setText(textArea.getText()+"(");
}
});
panel_1.add(button_15);
JButton button_16 = new JButton(")");
button_16.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.setText(textArea.getText()+")");
}
});
panel_1.add(button_16);
JButton button_17 = new JButton("^");
button_17.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.setText(textArea.getText()+"^");
}
});
panel_1.add(button_17);
JPanel panel_2 = new JPanel();
panel_2.setBounds(788, 162, 100, 246);
frame.getContentPane().add(panel_2);
panel_2.setLayout(new GridLayout(2, 0, 0, 0));
JButton button_14 = new JButton("="); //here's where the good stuff happens
button_14.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
evalString = textArea.getText(); //get eval string from textArea
while (evalString.contains("^")) //run the exponent replacer until they're all gone
{
evalString = evalExp.Evaluate(evalString);
}
double myDouble = 0; //double that answer will be stored in. we'll touch it up a little later in the code
try
{
myDouble = doEval.Calculate(evalString);
}
catch(ScriptException se)
{
textArea.setText("error"); //honestly this is just a formality. it wont ever happen and ScriptEngineManager isn't happy without catching ScriptException. If calc actually errors it displays 0
}
String calcOutput = Double.toString(myDouble);
if((calcOutput.charAt(calcOutput.length()-1) == '0') && (calcOutput.charAt(calcOutput.length()-2) == '.')) //check to see if answer ends in .0
{
calcOutput = calcOutput.substring(0, calcOutput.indexOf('.')); // if so, it removes it b4 displaying
}
textArea.setText(calcOutput); //obvious
}
});
panel_2.add(button_14);
JButton btnC_1 = new JButton("C"); //clears textArea when C button is pressed
btnC_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.setText("");
}
});
panel_2.add(btnC_1);
}
}