Java原始碼-溫度轉換小程式(Temperature Conversion)

需求簡述:使用Swing編寫GUI程式,實現攝氏溫度、華氏溫度及熱力學溫度(開爾文溫度)的相互轉換。

心得:在08年編寫的Applet小程式的基礎上修改而來。(Applet重構為Swing時,大部分程式碼可重用,很好!)

執行截圖:

程式碼如下:

//Convert between Celsius and Fahrenheit temperature
//Java how to program, 10/e, Exercise 12.12, 12.13
/*
12.12(Temperature Conversion) Write a temperature-conversion application that converts from
Fahrenheit to Celsius. The Fahrenheit temperature should be entered from the keyboard (via a
JTextField). A JLabel should be used to display the converted temperature. Use the following formula
for the conversion:
Celsius =5/9 × (Fahrenheit – 32)
12.13(Temperature-Conversion Modification) Enhance the temperature-conversion application
of Exercise 12.12 by adding the Kelvin temperature scale. The application should also allow the user
to make conversions between any two scales. Use the following formula for the conversion between
Kelvin and Celsius (in addition to the formula in Exercise 12.12):
Kelvin = Celsius   273.15
*/
import javax.swing.*;
import example.TextFieldFrame;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
public class TemperatureConverter extends JFrame {
double celsius,fahrenheit,kelvin;
String celsiusText,fahrenheitText,kelvinText;
JLabel CelsiusLabel,FahrenheitLabel, kelvinLabel;
JTextField celsiusField,FahrenheitField,kelvinField;
DecimalFormat twoDigits=new DecimalFormat("0.00");
public TemperatureConverter(){  
super("溫度轉換器(℃、℉、K)");
Container  container=getContentPane();
container.setLayout(new FlowLayout());
CelsiusLabel= new JLabel("請輸入攝氏溫度(℃):");
container.add(CelsiusLabel);
celsiusField=new JTextField(10);
container.add(celsiusField);
Handler h=new Handler();
celsiusField.addActionListener(h);
FahrenheitLabel= new JLabel("請輸入華氏溫度(℉):");
container.add(FahrenheitLabel);
FahrenheitField=new JTextField(10);
container.add(FahrenheitField);
FahrenheitField.addActionListener(h);
kelvinLabel= new JLabel("請輸入熱力學溫度(K):");
container.add(kelvinLabel);
kelvinField=new JTextField(10);
container.add(kelvinField);
kelvinField.addActionListener(h);
}
private class Handler implements ActionListener{
public void actionPerformed (ActionEvent event)
{
if (event.getSource()==celsiusField)
{
celsius=Double.parseDouble(celsiusField.getText());
fahrenheit=celsius*9.0/5.0 32;
fahrenheitText=twoDigits.format(fahrenheit);
FahrenheitField.setText(fahrenheitText);
kelvin=celsius 273.15;
kelvinText=twoDigits.format(kelvin);
kelvinField.setText(kelvinText);
}
else if (event.getSource()==FahrenheitField)
{
fahrenheit=Double.parseDouble(FahrenheitField.getText());
celsius=(fahrenheit-32)*5.0/9.0;
celsiusText=twoDigits.format(celsius);
celsiusField.setText(celsiusText);
kelvin=(fahrenheit-32)*5.0/9.0 273.15;
kelvinText=twoDigits.format(kelvin);
kelvinField.setText(kelvinText);
}
else if (event.getSource()==kelvinField)
{
kelvin=Double.parseDouble(kelvinField.getText());
celsius=kelvin-273.15;
celsiusText=twoDigits.format(celsius);
celsiusField.setText(celsiusText);
fahrenheit=celsius*9.0/5.0 32;
fahrenheitText=twoDigits.format(fahrenheit);
FahrenheitField.setText(fahrenheitText);
}
}
}
public static void main(String[] args)
{ 
TemperatureConverter tc = new TemperatureConverter(); 
tc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tc.setSize(350, 150); 
tc.setVisible(true); 
} 
}