Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion src/TemperatureConverter.java
Original file line number Diff line number Diff line change
@@ -1,2 +1,55 @@
public class TemperatureConverter {
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TempConverter extends JFrame {
private JTextField textField;
private JButton celsiusButton;
private JButton fahrenheitButton;

public TempConverter() {
createUI();
}

private void createUI() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300, 100);
setLocationRelativeTo(null);

textField = new JTextField(10);
celsiusButton = new JButton("To Celsius");
fahrenheitButton = new JButton("To Fahrenheit");

celsiusButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double tempFahr = Double.parseDouble(textField.getText());
textField.setText(String.format("%.2f", (tempFahr - 32) * 5.0 / 9.0));
}
});

fahrenheitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
double tempCels = Double.parseDouble(textField.getText());
textField.setText(String.format("%.2f", tempCels * 9.0 / 5.0 + 32.0));
}
});

setLayout(new FlowLayout());
add(textField);
add(celsiusButton);
add(fahrenheitButton);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TempConverter().setVisible(true);
}
});
}
}