-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMetricListener.java
168 lines (138 loc) · 4.84 KB
/
MetricListener.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
package swing;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class MetricListener implements ActionListener
{
private Converter c;
public MetricListener(Converter c){
this.c = c;
}
@Override
public void actionPerformed(ActionEvent e)
{
//Getting where the action takes place
JTextField txt = new JTextField();
txt = (JTextField) e.getSource();
//Variables to hold values for conversions
double meters = 0, feet = 0, miles = 0, inches = 0, cm = 0, km = 0;
//Used to get the name of JTextField that gets changed
String txtName = null;
txtName = txt.getName();
try{
//Extracting input from GUI
String input = null;
input = e.getActionCommand();
//Turing the taken in value into type double
double value = 0;
value = Double.parseDouble(input);
if (value < 0){
JOptionPane.showMessageDialog(null, "Error: Please enter a postivie number", "Error Message",
JOptionPane.ERROR_MESSAGE);
}
else if (value >= 0){
//The following if statements are seeing which JTextField is being changed
if(txtName.equals("meters")){
//Creating conversions from meters
meters = value;
feet = value * .3048;
miles = value * 0.000621371;
inches = value * 39.3701;
cm = value * 100;
km = value * .001;
//Applying the converted values to respected places
c.metersTxt.setText(Double.toString(meters));
c.feetTxt.setText(Double.toString(feet));
c.milesTxt.setText(Double.toString(miles));
c.inchesTxt.setText(Double.toString(inches));
c.cmTxt.setText(Double.toString(cm));
c.kmTxt.setText(Double.toString(km));
}
else if(txtName.equals("feet")){
//Creating conversions from feet
meters = value * .3048;
feet = value ;
miles = value * 0.000189394;
inches = value * 12;
cm = value * 30.48;
km = value * 0.0003048;
//Applying the converted values to respected places
c.metersTxt.setText(Double.toString(meters));
c.feetTxt.setText(Double.toString(feet));
c.milesTxt.setText(Double.toString(miles));
c.inchesTxt.setText(Double.toString(inches));
c.cmTxt.setText(Double.toString(cm));
c.kmTxt.setText(Double.toString(km));
}
else if(txtName.equals("miles")){
//Creating conversions from feet
meters = value * 1609.34;
feet = value * 5280;
miles = value;
inches = value * 63360;
cm = value * 160934;
km = value * 1.60934;
//Applying the converted values to respected places
c.metersTxt.setText(Double.toString(meters));
c.feetTxt.setText(Double.toString(feet));
c.milesTxt.setText(Double.toString(miles));
c.inchesTxt.setText(Double.toString(inches));
c.cmTxt.setText(Double.toString(cm));
c.kmTxt.setText(Double.toString(km));
}
else if(txtName.equals("inches")){
//Creating conversions from feet
meters = value * 0.0254;
feet = value * 0.0833333;
miles = value * .0000157828;
inches = value;
cm = value * 2.54;
km = value * .0000254;
//Applying the converted values to respected places
c.metersTxt.setText(Double.toString(meters));
c.feetTxt.setText(Double.toString(feet));
c.milesTxt.setText(Double.toString(miles));
c.inchesTxt.setText(Double.toString(inches));
c.cmTxt.setText(Double.toString(cm));
c.kmTxt.setText(Double.toString(km));
}
else if(txtName.equals("cm")){
//Creating conversions from feet
meters = value * 0.01;
feet = value * 0.0328084;
miles = value * .00000621371;
inches = value * 0.393701;
cm = value;
km = value * .00001;
//Applying the converted values to respected places
c.metersTxt.setText(Double.toString(meters));
c.feetTxt.setText(Double.toString(feet));
c.milesTxt.setText(Double.toString(miles));
c.inchesTxt.setText(Double.toString(inches));
c.cmTxt.setText(Double.toString(cm));
c.kmTxt.setText(Double.toString(km));
}
else if(txtName.equals("km")){
//Creating conversions from feet
meters = value * 1000f;
feet = value * 3280.84;
miles = value * 0.621371;
inches = value * 39370.1;
cm = value * 100000;
km = value;
//Applying the converted values to respected places
c.metersTxt.setText(Double.toString(meters));
c.feetTxt.setText(Double.toString(feet));
c.milesTxt.setText(Double.toString(miles));
c.inchesTxt.setText(Double.toString(inches));
c.cmTxt.setText(Double.toString(cm));
c.kmTxt.setText(Double.toString(km));
}
}
} catch(NumberFormatException ex){
JOptionPane.showMessageDialog(null, "Error: Please enter a number", "Error Message",
JOptionPane.ERROR_MESSAGE);
}
}
}