-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemperatureConverterUI.java
More file actions
145 lines (123 loc) · 5.49 KB
/
TemperatureConverterUI.java
File metadata and controls
145 lines (123 loc) · 5.49 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class TemperatureConverterUI extends JFrame {
private JTextField inputField;
private JLabel resultLabel1;
private JLabel resultLabel2;
private JComboBox<String> sourceUnitComboBox;
private DecimalFormat df = new DecimalFormat("#.##");
public TemperatureConverterUI() {
// Setup the frame
setTitle("Temperature Converter");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// Create the main panel with some padding
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout(10, 10));
mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
// Create title label
JLabel titleLabel = new JLabel("Temperature Converter", JLabel.CENTER);
titleLabel.setFont(new Font("Arial", Font.BOLD, 20));
mainPanel.add(titleLabel, BorderLayout.NORTH);
// Create input panel
JPanel inputPanel = new JPanel(new GridLayout(3, 2, 10, 10));
JLabel sourceLabel = new JLabel("Source Temperature:");
inputPanel.add(sourceLabel);
inputField = new JTextField();
inputField.setFont(new Font("Arial", Font.PLAIN, 14));
inputPanel.add(inputField);
JLabel unitLabel = new JLabel("Source Unit:");
inputPanel.add(unitLabel);
sourceUnitComboBox = new JComboBox<>(new String[]{"Celsius (°C)", "Kelvin (K)", "Fahrenheit (°F)"});
inputPanel.add(sourceUnitComboBox);
JButton convertButton = new JButton("Convert");
convertButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
convertTemperature();
}
});
inputPanel.add(convertButton);
JButton clearButton = new JButton("Clear");
clearButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
inputField.setText("");
resultLabel1.setText("Result 1: ");
resultLabel2.setText("Result 2: ");
}
});
inputPanel.add(clearButton);
mainPanel.add(inputPanel, BorderLayout.CENTER);
// Create result panel
JPanel resultPanel = new JPanel(new GridLayout(2, 1, 5, 5));
resultPanel.setBorder(BorderFactory.createTitledBorder("Results"));
resultLabel1 = new JLabel("Result 1: ");
resultLabel1.setFont(new Font("Arial", Font.PLAIN, 14));
resultPanel.add(resultLabel1);
resultLabel2 = new JLabel("Result 2: ");
resultLabel2.setFont(new Font("Arial", Font.PLAIN, 14));
resultPanel.add(resultLabel2);
mainPanel.add(resultPanel, BorderLayout.SOUTH);
// Add the main panel to the frame
add(mainPanel);
// Make enter key work for conversion
inputField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
convertTemperature();
}
}
});
}
private void convertTemperature() {
try {
double temperature = Double.parseDouble(inputField.getText());
int selectedUnit = sourceUnitComboBox.getSelectedIndex();
switch (selectedUnit) {
case 0: // Celsius
double kelvin = temperature + 273.15;
double fahrenheit = (temperature * 9/5) + 32;
resultLabel1.setText("Kelvin (K): " + df.format(kelvin));
resultLabel2.setText("Fahrenheit (°F): " + df.format(fahrenheit));
break;
case 1: // Kelvin
double celsius = temperature - 273.15;
fahrenheit = (celsius * 9/5) + 32;
resultLabel1.setText("Celsius (°C): " + df.format(celsius));
resultLabel2.setText("Fahrenheit (°F): " + df.format(fahrenheit));
break;
case 2: // Fahrenheit
celsius = (temperature - 32) * 5/9;
kelvin = celsius + 273.15;
resultLabel1.setText("Celsius (°C): " + df.format(celsius));
resultLabel2.setText("Kelvin (K): " + df.format(kelvin));
break;
}
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(this,
"Please enter a valid number",
"Input Error",
JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args) {
// Set system look and feel
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
// Run the application
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TemperatureConverterUI().setVisible(true);
}
});
}
}