// graphical user interface components
JLabel lblToCelsius, lblToFahrenheit, lblFromCelsius, lblFromFahrenheit;
JTextField txtToCelsius, txtToFahrenheit, txtFromCelsius,
txtFromFahrenheit;
JButton cmdToCelsius, cmdToFahrenheit;
JPanel panConvert;
public void init( )
{
// setup graphical user interface
components
JPanel panAdjust = new JPanel();
panAdjust.setLayout( new GridLayout(2, 5, 4, 4) );
// developing first row of the conversion form
lblFromCelsius = new JLabel( "Enter the degrees Celsius: ",
SwingConstants.RIGHT );
// adds components across the row first
panAdjust.add( lblFromCelsius );
txtFromCelsius = new JTextField( 5 );
panAdjust.add( txtFromCelsius );
cmdToFahrenheit = new JButton( "Convert to Fahrenheit" );
cmdToFahrenheit.addActionListener(this);
panAdjust.add( cmdToFahrenheit );
lblToFahrenheit = new JLabel( " in Fahrenheit is: ",
SwingConstants.RIGHT );
panAdjust.add( lblToFahrenheit );
txtToFahrenheit = new JTextField( 5 );
txtToFahrenheit.setEditable(false);
panAdjust.add( txtToFahrenheit );
// developing the second row of the conversion
form
lblFromFahrenheit = new JLabel( "Enter the degrees Fahrenheit: ",
SwingConstants.RIGHT );
// adds them across the second row
panAdjust.add( lblFromFahrenheit );
txtFromFahrenheit = new JTextField( 5 );
panAdjust.add( txtFromFahrenheit );
cmdToCelsius = new JButton( "Convert to Celsius" );
cmdToCelsius.addActionListener(this);
panAdjust.add( cmdToCelsius );
lblToCelsius = new JLabel( " in Celsius is ", SwingConstants.RIGHT);
panAdjust.add( lblToCelsius );
txtToCelsius = new JTextField( 5 );
txtToCelsius.setEditable(false);
panAdjust.add( txtToCelsius );
setContentPane(panAdjust);
} // end method init( )
// the actionEvent handler
public void actionPerformed(ActionEvent userClick)
{
double degreesCelsius = 0.0;
double degreesFahrenheit = 0.0;
boolean validInput = true;
// if statement to determine source
// of user event/click
if (userClick.getSource() == cmdToFahrenheit)
{
try
{
degreesCelsius = Double.parseDouble(txtFromCelsius.getText());
}
catch (NumberFormatException nfeFloat)
{
JOptionPane.showMessageDialog(null, "You need to enter a
number", "Input Error", JOptionPane.ERROR_MESSAGE);
validInput = false;
txtFromCelsius.setText("");
}
if (validInput)
convertToFahrenheit(degreesCelsius);
}
else if (userClick.getSource() == cmdToCelsius)
{
try
{
degreesFahrenheit =
Double.parseDouble(txtFromFahrenheit.getText());
}
catch (NumberFormatException nfeFloat)
{
JOptionPane.showMessageDialog(null, "You need to enter a
number", "Input Error", JOptionPane.ERROR_MESSAGE);
validInput = false;
txtFromFahrenheit.setText("");
}
if (validInput)
convertToCelsius(degreesFahrenheit);
}
} // end actionPerformed( )
public void convertToFahrenheit(double celsiusTemp)
{
double fahrenheitTemp;
fahrenheitTemp = (1.8 * celsiusTemp) + 32.0;
txtToFahrenheit.setText(Double.toString(fahrenheitTemp));
} // end method convertToFahrenheit( )
public void convertToCelsius(double fahrenheitTemp)
{
double celsiusTemp;
celsiusTemp = (fahrenheitTemp - 32.0)*5.0/9.0;
txtToCelsius.setText(Double.toString(celsiusTemp));
} // end method convertToCelsius( )