-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTempConversion.java
More file actions
executable file
·86 lines (68 loc) · 2.65 KB
/
Copy pathTempConversion.java
File metadata and controls
executable file
·86 lines (68 loc) · 2.65 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TempConversion extends JFrame
{
private JLabel celsiusLabel;
private JLabel fahrenheitLabel;
private JTextField celsiusTF;
private JTextField fahrenheitTF;
private CelsHandler celsiusHandler;
private FahrHandler fahrenheitHandler;
private static final int WIDTH = 500;
private static final int HEIGHT = 75;
private static final double FTOC = 5.0/9.0;
private static final double CTOF = 9.0/5.0;
private static final int OFFSET = 32;
public TempConversion()
{
setTitle("Temperature Conversion");
Container c = getContentPane();
c.setLayout(new GridLayout(1,4));
celsiusLabel = new JLabel("Temp in Celsius: ",
SwingConstants.RIGHT);
fahrenheitLabel = new JLabel("Temp in Fahrenheit: ",
SwingConstants.RIGHT);
celsiusTF = new JTextField(7);
fahrenheitTF = new JTextField(7);
c.add(celsiusLabel);
c.add(celsiusTF);
c.add(fahrenheitLabel);
c.add(fahrenheitTF);
celsiusHandler = new CelsHandler();
fahrenheitHandler = new FahrHandler();
celsiusTF.addActionListener(celsiusHandler);
fahrenheitTF.addActionListener(fahrenheitHandler);
setSize (WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private class CelsHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double celsius, fahrenheit;
celsius =
Double.parseDouble(celsiusTF.getText());
fahrenheit = celsius * CTOF + OFFSET;
fahrenheitTF.setText(String.format("%.2f", fahrenheit));
}
}
private class FahrHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double celsius, fahrenheit;
fahrenheit =
Double.parseDouble(fahrenheitTF.getText());
celsius = (fahrenheit - OFFSET) * FTOC;
celsiusTF.setText(String.format("%.2f", celsius));
}
}
public static void main(String[] args)
{
TempConversion tempConv = new TempConversion();
tempConv.setLocationRelativeTo(null);
}
}
//END OF CODE