Skip to content

Instantly share code, notes, and snippets.

@raphaelsaunier
Created December 14, 2011 10:07
Show Gist options
  • Save raphaelsaunier/1475972 to your computer and use it in GitHub Desktop.
Save raphaelsaunier/1475972 to your computer and use it in GitHub Desktop.
TemperatureConverterGUI.java
// TemperatureConverterGUI.java
// Author: Laurencia Walker-Fooks
// Program last changed: 7 December 2011
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionListener;
public class TemperatureConverterGUI extends JFrame
{
private JLabel promptLabel;
private JLabel answerLabel;
private int statusFlag = -1;
private JPanel inputPanel = new JPanel();
private JPanel answerPanel = new JPanel();
private JPanel menuPanel = new JPanel();
private JMenu conversionMenu;
private JMenuBar conversionMenuBar = new JMenuBar();
private JTextField userInputField = new JTextField("",6);
private JButton conversionButton = new JButton("GO");
private JLabel instructionLabel = new JLabel("(Select conversion type first.)");
public TemperatureConverterGUI ()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new GridLayout(3, 1));
this.promptLabel = new JLabel("Select conversion type: ",JLabel.LEFT);
this.menuPanel.add(this.promptLabel);
this.conversionMenu = new JMenu("(no conversion type selected)");
this.conversionMenuBar.add(conversionMenu);
this.menuPanel.add(this.conversionMenuBar);
this.add(this.menuPanel);
userInputField.setEditable(false);
conversionButton.addActionListener(new TemperatureListener());
this.inputPanel.add(this.instructionLabel);
this.inputPanel.add(this.userInputField);
this.inputPanel.add(this.conversionButton);
this.add(this.inputPanel);
this.answerLabel = new JLabel(" ",JLabel.CENTER);
this.answerPanel.add(this.answerLabel);
this.add(this.answerPanel);
Container container = getContentPane();
container.add(answerPanel, BorderLayout.SOUTH);
for (int i = 0; i < 2; i++)
{
if (i == 0)
{
JMenuItem aConversion = new JMenuItem("Centigrade To Fahrenheit");
aConversion.addActionListener(new TemperatureListener());
this.conversionMenu.add(aConversion);
}
else
{
JMenuItem aConversion = new JMenuItem("Fahrenheit to Centigrade");
aConversion.addActionListener(new TemperatureListener());
this.conversionMenu.add(aConversion);
}
}
}
public boolean isDouble (String input)
{
try
{
Double.parseDouble(input);
return true;
}
catch (NumberFormatException exception)
{
return false;
//answerLabel.setText("SYSTEM ERROR: Input Numerical Number Please.");
}
}
private class TemperatureListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String temp = e.getActionCommand();
if (temp == "Centigrade To Fahrenheit")
{
conversionMenu.setText(e.getActionCommand());
statusFlag = 1;
}
else if (temp == "Fahrenheit to Centigrade")
{
conversionMenu.setText(e.getActionCommand());
statusFlag = 0;
}
userInputField.setEditable(true);
String userInput = userInputField.getText();
if (isDouble(userInput) == true)
{
//boolean isDouble = Double.parseDouble(e.getActionCommand());
}
else
{
answerLabel.setText("SYSTEM ERROR: Input Numerical Number Please.");
userInputField.setText("");
}
if (statusFlag == 1)
{
if (isDouble(userInput) == true)
{
double celsius = Double.parseDouble(userInput);
double fahrenheit = (celsius * 9.0/5.0) + 32;
//answerLabel.setText(String.format("%.2f", fahrenheit));
answerLabel.setText(String.format("%.2f", celsius) + " degrees Centigrade is " + String.format("%.2f", fahrenheit) + " degrees Fahrenheit.");
}
else
answerLabel.setText("SYSTEM ERROR: Input Numerical Number Please.");
}
else if (statusFlag == 0)
{
if (isDouble(userInput) == true)
{
double fahrenheit = Double.parseDouble(userInput);
double celsius = (fahrenheit - 32.0) * 5.0 / 9.0;
//answerLabel.setText(String.format("%.2f", celsius));
answerLabel.setText(String.format("%.2f", fahrenheit) + " degrees Centigrade is " + String.format("%.2f", celsius) + " degrees Fahrenheit.");
}
else
answerLabel.setText("SYSTEM ERROR: Input Numerical Number Please.");
}
}
}
public static void main (String[] args)
{
TemperatureConverterGUI win = new TemperatureConverterGUI();
win.setTitle("Temperature Converter Window");
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.pack();
win.setVisible(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment