forked from rick2785/JavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaLesson23.java
More file actions
363 lines (202 loc) · 8.31 KB
/
JavaLesson23.java
File metadata and controls
363 lines (202 loc) · 8.31 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import javax.swing.*;
import java.awt.event.*;
// New event listener that monitors changing values for components
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
// Allows me to format the numbers
import java.text.NumberFormat;
// Allows me to edit borders on panels
import javax.swing.border.*;
public class JavaLesson23 extends JFrame{
JButton button1;
JLabel label1, label2, label3;
JTextField textField1, textField2;
JCheckBox dollarSign, commaSeparator;
JRadioButton addNums, subtractNums, multNums, divideNums;
JSlider howManyTimes;
double number1, number2, totalCalc;
public static void main(String[] args){
new JavaLesson23();
}
public JavaLesson23(){
// Define the size of the frame
this.setSize(400, 400);
// Opens the frame in the middle of the screen
this.setLocationRelativeTo(null);
// Define how the frame exits (Click the Close Button)
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Define the title for the frame
this.setTitle("My Third Frame");
// The JPanel contains all of the components for your frame
JPanel thePanel = new JPanel();
// ---------------------------------------------------------
// Create a button with Click Here on it
button1 = new JButton("Calculate");
// Create an instance of ListenForEvents to handle events
ListenForButton lForButton = new ListenForButton();
// Tell Java that you want to be alerted when an event
// occurs on the button
button1.addActionListener(lForButton);
thePanel.add(button1);
// How to add a label --------------------------
label1 = new JLabel("Number 1");
thePanel.add(label1);
// How to add a text field ----------------------
textField1 = new JTextField("", 5);
thePanel.add(textField1);
// How to add a label --------------------------
label2 = new JLabel("Number 2");
thePanel.add(label2);
// How to add a text field ----------------------
textField2 = new JTextField("", 5);
thePanel.add(textField2);
// How to add checkboxes ------------------------
dollarSign = new JCheckBox("Dollars");
commaSeparator = new JCheckBox("Commas");
thePanel.add(dollarSign);
// By putting true in here it is selected by default
thePanel.add(commaSeparator, true);
// Creates radio buttons with default labels
addNums = new JRadioButton("Add");
subtractNums = new JRadioButton("Subtract");
multNums = new JRadioButton("Multiply");
divideNums = new JRadioButton("Divide");
// Creates a group that will contain radio buttons
// You do this so that when 1 is selected the others
// are deselected
ButtonGroup operation = new ButtonGroup();
// Add radio buttons to the group
operation.add(addNums);
operation.add(subtractNums);
operation.add(multNums);
operation.add(divideNums);
// Create a new panel to hold radio buttons
JPanel operPanel = new JPanel();
// Surround radio button panel with a border
// You can define different types of borders
// createEtchedBorder, createLineBorder, createTitledBorder
// createLoweredBevelBorder, createRaisedBevelBorder
Border operBorder = BorderFactory.createTitledBorder("Operation");
// Set the border for the panel
operPanel.setBorder(operBorder);
// Add the radio buttons to the panel
operPanel.add(addNums);
operPanel.add(subtractNums);
operPanel.add(multNums);
operPanel.add(divideNums);
// Selects the add radio button by default
addNums.setSelected(true);
// Add the panel to the main panel
// You don't add the group
thePanel.add(operPanel);
// How to create a slider ----------------
label3 = new JLabel("Perform How Many Times?");
thePanel.add(label3);
// Creates a slider with a min value of 0 thru 99
// and an initial value of 1
howManyTimes = new JSlider(0, 99, 1);
// Defines the minimum space between ticks
howManyTimes.setMinorTickSpacing(1);
// Defines the minimum space between major ticks
howManyTimes.setMajorTickSpacing(10);
// Says to draw the ticks on the slider
howManyTimes.setPaintTicks(true);
// Says to draw the tick labels on the slider
howManyTimes.setPaintLabels(true);
// Create an instance of ListenForEvents to handle events
ListenForSlider lForSlider = new ListenForSlider();
// Tell Java that you want to be alerted when an event
// occurs on the slider
howManyTimes.addChangeListener(lForSlider);
thePanel.add(howManyTimes);
this.add(thePanel);
this.setVisible(true);
// Gives focus to the textfield
textField1.requestFocus();
}
private class ListenForButton implements ActionListener{
// This method is called when an event occurs
public void actionPerformed(ActionEvent e){
// Check if the source of the event was the button
if(e.getSource() == button1){
// getText returns a String so you have to parse it
// into a double in this situation
try{
number1 = Double.parseDouble(textField1.getText());
number2 = Double.parseDouble(textField2.getText());
}
catch(NumberFormatException excep){
// JOptionPane displays a popup on the screen
// (parentComponent, message, title, error icon)
// Error Icons: WARNING_MESSAGE, QUESTION_MESSAGE, PLAIN_MESSAGE
JOptionPane.showMessageDialog(JavaLesson23.this, "Please Enter the Right Info", "Error", JOptionPane.ERROR_MESSAGE);
System.exit(0); // Closes the Java app
}
if(addNums.isSelected()) { totalCalc = addNumbers(number1, number2, howManyTimes.getValue());
} else if(subtractNums.isSelected()) { totalCalc = subtractNumbers(number1, number2, howManyTimes.getValue());
} else if(multNums.isSelected()) { totalCalc = multiplyNumbers(number1, number2, howManyTimes.getValue());
} else { totalCalc = divideNumbers(number1, number2, howManyTimes.getValue()); }
// If the dollar is selected in the checkbox print the number as currency
if(dollarSign.isSelected()) {
// Defines that you want to format a number with $ and commas
NumberFormat numFormat = NumberFormat.getCurrencyInstance();
JOptionPane.showMessageDialog(JavaLesson23.this, numFormat.format(totalCalc), "Solution", JOptionPane.INFORMATION_MESSAGE);
}
// If the comma is selected in the checkbox print the number with commas
else if(commaSeparator.isSelected()) {
// Defines that you want to format a number with commas
NumberFormat numFormat = NumberFormat.getNumberInstance();
JOptionPane.showMessageDialog(JavaLesson23.this, numFormat.format(totalCalc), "Solution", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(JavaLesson23.this, totalCalc, "Solution", JOptionPane.INFORMATION_MESSAGE);
}
}
}
}
// Implements ActionListener so it can react to events on components
private class ListenForSlider implements ChangeListener{
@Override
public void stateChanged(ChangeEvent e) {
// Check if the source of the event was the button
if(e.getSource() == howManyTimes){
label3.setText("Perform How Many Times? " + howManyTimes.getValue() );
}
}
}
public static double addNumbers(double number1, double number2, int howMany){
double total = 0;
int i = 1;
while(i <= howMany){
total = total + (number1 + number2);
i++;
}
return total;
}
public static double subtractNumbers(double number1, double number2, int howMany){
double total = 0;
int i = 1;
while(i <= howMany){
total = total + (number1 - number2);
i++;
}
return total;
}
public static double multiplyNumbers(double number1, double number2, int howMany){
double total = 0;
int i = 1;
while(i <= howMany){
total = total + (number1 * number2);
i++;
}
return total;
}
public static double divideNumbers(double number1, double number2, int howMany){
double total = 0;
int i = 1;
while(i <= howMany){
total = total + (number1 / number2);
i++;
}
return total;
}
}