需求简述:
使用Swing编写GUI程序,实现摄氏温度、华氏温度及热力学温度(开尔文温度)的相互转换。
注:在08年本人编写的Applet小程序的基础上修改而来。(Applet重构为Swing时,大部分代码可重用,很好!)
英文需求(Java How to Program第10版第12章(GUI控件第一部分)的课后习题):
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 = × (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 |
运行截屏:
代码如下:
//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);
}
}