Java源码-温度转换小程序(Temperature Conversion)

需求简述:

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

}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值