1
|
|
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
package example;
|
11
|
|
|
12
|
|
import java.awt.BorderLayout;
|
13
|
|
import java.awt.Dimension;
|
14
|
|
import java.awt.GridLayout;
|
15
|
|
import java.awt.event.ActionEvent;
|
16
|
|
import java.awt.event.ActionListener;
|
17
|
|
|
18
|
|
import java.text.DecimalFormat;
|
19
|
|
import java.text.MessageFormat;
|
20
|
|
|
21
|
|
import javax.swing.BorderFactory;
|
22
|
|
import javax.swing.ButtonGroup;
|
23
|
|
import javax.swing.JButton;
|
24
|
|
import javax.swing.JFrame;
|
25
|
|
import javax.swing.JLabel;
|
26
|
|
import javax.swing.JMenu;
|
27
|
|
import javax.swing.JMenuBar;
|
28
|
|
import javax.swing.JPanel;
|
29
|
|
import javax.swing.JRadioButtonMenuItem;
|
30
|
|
import javax.swing.JTextField;
|
31
|
|
import javax.swing.SwingConstants;
|
32
|
|
import javax.swing.UIManager;
|
33
|
|
|
34
|
|
public class CelsiusConverter extends JPanel {
|
35
|
|
private static final int NPRECISIONS = 5;
|
36
|
|
private int precision;
|
37
|
|
private JLabel celsiusLabel;
|
38
|
|
private JLabel fahrLabel;
|
39
|
|
private JTextField inputText;
|
40
|
|
|
41
|
|
|
42
|
0
|
public CelsiusConverter() {
|
43
|
|
|
44
|
0
|
final int MARGIN = 2;
|
45
|
0
|
setLayout(new GridLayout(0, 2, MARGIN, MARGIN));
|
46
|
0
|
setBorder(BorderFactory.createEmptyBorder(MARGIN, MARGIN, MARGIN, MARGIN));
|
47
|
|
|
48
|
0
|
JPanel left = new JPanel(new BorderLayout());
|
49
|
0
|
left.setBorder(BorderFactory.createEtchedBorder());
|
50
|
0
|
add(left);
|
51
|
|
|
52
|
0
|
JPanel right = new JPanel(new BorderLayout());
|
53
|
0
|
right.setBorder(BorderFactory.createEtchedBorder());
|
54
|
0
|
add(right);
|
55
|
|
|
56
|
|
|
57
|
0
|
inputText = new JTextField(2);
|
58
|
0
|
final JButton convertTemp = new JButton(lookupString("conversion.button.text"));
|
59
|
0
|
celsiusLabel = new JLabel(lookupString("input.label.text"), SwingConstants.LEFT);
|
60
|
0
|
fahrLabel = new JLabel(lookupString("output.label.text"), SwingConstants.LEFT);
|
61
|
|
|
62
|
|
|
63
|
0
|
left.add(inputText, BorderLayout.NORTH);
|
64
|
0
|
left.add(convertTemp, BorderLayout.SOUTH);
|
65
|
|
|
66
|
0
|
right.add(celsiusLabel, BorderLayout.NORTH);
|
67
|
0
|
right.add(fahrLabel, BorderLayout.SOUTH);
|
68
|
|
|
69
|
0
|
celsiusLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
|
70
|
0
|
fahrLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
|
71
|
|
|
72
|
|
|
73
|
0
|
convertTemp.addActionListener(new ActionListener() {
|
74
|
0
|
public void actionPerformed(ActionEvent event) {
|
75
|
0
|
updateLabels();
|
76
|
|
}
|
77
|
|
});
|
78
|
0
|
inputText.addActionListener(new ActionListener() {
|
79
|
0
|
public void actionPerformed(ActionEvent e) {
|
80
|
0
|
convertTemp.doClick();
|
81
|
|
}
|
82
|
|
});
|
83
|
|
}
|
84
|
|
|
85
|
|
|
86
|
0
|
public static double convert(double celsius) {
|
87
|
0
|
return celsius * 9 / 5 + 32;
|
88
|
|
}
|
89
|
|
|
90
|
|
|
91
|
0
|
public static String lookupString(String key) {
|
92
|
0
|
return CelsiusConverterStrings.getString(key);
|
93
|
|
}
|
94
|
|
|
95
|
|
|
96
|
0
|
public static String formatOutput(String format, double value,
|
97
|
|
int precision) {
|
98
|
0
|
MessageFormat f = new MessageFormat(format);
|
99
|
0
|
String pattern = "#";
|
100
|
0
|
if (precision > 0)
|
101
|
0
|
pattern += ".";
|
102
|
0
|
while (precision-- > 0)
|
103
|
0
|
pattern += "#";
|
104
|
0
|
DecimalFormat dfmt = new DecimalFormat(pattern);
|
105
|
0
|
return f.format(new Object[] { dfmt.format(value) });
|
106
|
|
}
|
107
|
|
|
108
|
|
|
109
|
0
|
public static String fahrenheitOutput(double value, int precision) {
|
110
|
0
|
return formatOutput(lookupString("F"), value, precision);
|
111
|
|
}
|
112
|
|
|
113
|
|
|
114
|
0
|
public static String celsiusOutput(double value, int precision) {
|
115
|
0
|
return formatOutput(lookupString("C"), value, precision);
|
116
|
|
}
|
117
|
|
|
118
|
0
|
private void updateLabels() {
|
119
|
0
|
String in = inputText.getText();
|
120
|
0
|
try {
|
121
|
|
|
122
|
0
|
double celsius = Double.parseDouble(in);
|
123
|
0
|
celsiusLabel.setText(formatOutput(lookupString("C"),
|
124
|
|
celsius,
|
125
|
|
precision));
|
126
|
0
|
double fahr = convert(celsius);
|
127
|
0
|
fahrLabel.setText(formatOutput(lookupString("F"), fahr,
|
128
|
|
precision));
|
129
|
|
} catch (Exception e){
|
130
|
0
|
inputText.selectAll();
|
131
|
|
}
|
132
|
|
}
|
133
|
|
|
134
|
0
|
private JMenuBar createMenuBar() {
|
135
|
|
|
136
|
0
|
JMenuBar menuBar = new JMenuBar();
|
137
|
0
|
JMenu menu = new JMenu(lookupString("menu.options"));
|
138
|
0
|
menuBar.add(menu);
|
139
|
0
|
JMenu submenu = new JMenu(lookupString("menu.precision"));
|
140
|
0
|
menu.add(submenu);
|
141
|
0
|
ActionListener listener = new ActionListener() {
|
142
|
0
|
public void actionPerformed(ActionEvent e) {
|
143
|
0
|
if (((JRadioButtonMenuItem)e.getSource()).isSelected()) {
|
144
|
0
|
precision = Integer.parseInt(e.getActionCommand());
|
145
|
0
|
updateLabels();
|
146
|
|
}
|
147
|
|
}
|
148
|
|
};
|
149
|
0
|
ButtonGroup group = new ButtonGroup();
|
150
|
0
|
for (int i=0;i < NPRECISIONS;i++) {
|
151
|
0
|
JRadioButtonMenuItem item =
|
152
|
|
new JRadioButtonMenuItem(String.valueOf(i), i==precision);
|
153
|
0
|
item.setActionCommand(String.valueOf(i));
|
154
|
0
|
item.addActionListener(listener);
|
155
|
0
|
group.add(item);
|
156
|
0
|
submenu.add(item);
|
157
|
|
}
|
158
|
|
|
159
|
0
|
return menuBar;
|
160
|
|
}
|
161
|
|
|
162
|
|
|
163
|
|
|
164
|
|
|
165
|
|
|
166
|
0
|
public void enframe(JFrame frame) {
|
167
|
0
|
frame.setTitle(lookupString("frame.title"));
|
168
|
|
|
169
|
0
|
frame.setContentPane(this);
|
170
|
0
|
frame.setJMenuBar(createMenuBar());
|
171
|
|
|
172
|
|
|
173
|
0
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
174
|
|
|
175
|
0
|
frame.pack();
|
176
|
0
|
Dimension d = frame.getSize();
|
177
|
0
|
d.width = Math.max(d.width, 350);
|
178
|
0
|
frame.setSize(d);
|
179
|
|
}
|
180
|
|
|
181
|
|
|
182
|
0
|
public static void main(String[] args) {
|
183
|
0
|
try {
|
184
|
0
|
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
|
185
|
|
}
|
186
|
|
catch (Exception e) {
|
187
|
0
|
e.printStackTrace();
|
188
|
0
|
System.exit(1);
|
189
|
|
}
|
190
|
0
|
CelsiusConverter converter = new CelsiusConverter();
|
191
|
0
|
JFrame frame = new JFrame();
|
192
|
0
|
converter.enframe(frame);
|
193
|
0
|
frame.setVisible(true);
|
194
|
|
}
|
195
|
|
|
196
|
|
}
|
197
|
|
|