-
Notifications
You must be signed in to change notification settings - Fork 0
/
TTT_TemperatureConverter.java
160 lines (137 loc) · 5.19 KB
/
TTT_TemperatureConverter.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
//Thessalonica Turnbull
//Advanced Java Spring 2023
//Created March 29, 2023
//Temperature Converter Widget
//
//worked with Kyle Nguyen to complete this program
//
//allows user to convert temperatures between fahrenheit, celsius, and kelvin
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TTT_TemperatureConverter extends JPanel implements ActionListener{
//global variables
JComboBox<String> fromMenu;
String[] fromList;
JComboBox<String> toMenu;
String[] toList;
TextField fromText, answer;
JButton convertButton, resetButton, exitButton;
//cconstructor
TTT_TemperatureConverter(){
//for layout
////GridLayout - used for all panels
this.setLayout(new GridLayout(4,1));
JPanel titlePanel = new JPanel();
JPanel fromPanel = new JPanel();
JPanel toPanel = new JPanel();
JPanel buttonPanel = new JPanel();
Font appFontLarge = new Font("Arial", Font.PLAIN, 50);
Font appFontSmall = new Font("Arial", Font.PLAIN, 18);
//title panel
JLabel titleLabel = new JLabel("Temperature Converter");
titleLabel.setFont(appFontLarge);;
titlePanel.add(titleLabel);
//from panel
fromList = new String[] {"Fahrenheit", "Celsius", "Kelvin"};
JLabel fromLabel = new JLabel("Original temperature: "); //creates label for from
fromLabel.setFont(appFontSmall);
fromText = new TextField(); //creates fromText textbox
fromText.setPreferredSize(new Dimension(200,30));
fromText.setFont(appFontSmall);
fromText.setForeground(Color.BLACK);
fromMenu = new JComboBox<String>(fromList); //creates fromMenu dropdown box
fromMenu.setFont(appFontSmall);
fromPanel.add(fromLabel);
fromPanel.add(fromText);
fromPanel.add(fromMenu);
//to panel
toList = new String[] {"Fahrenheit", "Celsius", "Kelvin"};
JLabel toLabel = new JLabel("Converted temperature: "); //creates label for from
toLabel.setFont(appFontSmall);
answer = new TextField(); //creates fromText textbox
answer.setPreferredSize(new Dimension(200,30));
answer.setFont(appFontSmall);
answer.setForeground(Color.BLACK);
toMenu = new JComboBox<String>(fromList); //creates fromMenu dropdown box
toMenu.setFont(appFontSmall);
fromPanel.add(toLabel);
fromPanel.add(answer);
fromPanel.add(toMenu);
//button panel
convertButton = new JButton("Convert");
resetButton = new JButton("Reset");
exitButton = new JButton("Exit");
buttonPanel.add(convertButton); //adds each item below to buttonPanel
buttonPanel.add(resetButton);
buttonPanel.add(exitButton);
//add JPanels to GridLayout
this.add(titlePanel);
this.add(fromPanel);
this.add(toPanel);
this.add(buttonPanel);
//actionListener handles
fromMenu.addActionListener(this);
toMenu.addActionListener(this);
convertButton.addActionListener(this);
resetButton.addActionListener(this);
exitButton.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == fromMenu) {//clears textboxes if fromMenu selection changes
fromText.setText(null);
answer.setText(null);
}
if(e.getSource() == toMenu) {//clears textboxes if toMenu selection changes
fromText.setText(null);
answer.setText(null);
}
if(e.getSource() == exitButton) {//exits program
System.exit(0);
}
if(e.getSource() == resetButton) {//resets all fields
fromMenu.setSelectedIndex(0);
toMenu.setSelectedIndex(0);
fromText.setText(null);
answer.setText(null);
}
if(e.getSource() == convertButton) {//does conversions based on user input/selection
if (fromMenu.getSelectedIndex() < 0 || fromText.getText().equals("")) {
JOptionPane.showMessageDialog(null, "Invalid Input", "Getting Error", JOptionPane.ERROR_MESSAGE);
} else {
double originalAmount = Double.parseDouble(fromText.getText());
double amountInF = 0.0;
switch (fromMenu.getSelectedItem().toString()) {
case "Fahrenheit":
amountInF = originalAmount;
break;
case "Celsius":
amountInF = (originalAmount * (1.8)) + 32;
break;
case "Kelvin":
amountInF = (originalAmount - 273.15) * 1.8 + 32;
break;
default:
amountInF = 0.0;
}
double newamount = 0.0;
switch (toMenu.getSelectedItem().toString()) {
case "Fahrenheit":
newamount = amountInF;
break;
case "Celsius":
newamount = (amountInF - 32) * (5.0/9);
break;
case "Kelvin":
newamount = (amountInF + 459.67) * (5.0/9);
break;
default:
newamount = amountInF = 0.0;
}
String amount = String.format("%.2f", newamount);
answer.setText(amount);
}
}
}
}