Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update TemperatureConverter.java #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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) {
int tempFahr = Integer.parseInt(textField.getText());
textField.setText(String.valueOf((tempFahr - 32) * 5 / 9));
}
});

fahrenheitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int tempCels = Integer.parseInt(textField.getText());
textField.setText(String.valueOf(tempCels * 9 / 5 + 32));
}
});

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);
}
});
}
}