温度の変換を行う必要があります。華氏、摂氏、ケルビンのいずれかに 1 つの値を入力して [計算] を押すと、他の 2 つの値が計算されます。計算とクリアの2つのボタンしかありません。計算ボタンを押したときに、1 つの値を入力して他の 2 つの値を計算するにはどうすればよいでしょうか?
private class ButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
//declare three constant values for absolute zero
double A_ZERO_F = -459.67;
double A_ZERO_C = -273.15;
double A_ZERO_K = 0.0;
// read the data from any of the fields as String types
//We use the JTextField method getText() to do the read
String fahrString = fahrFld.getText();
String celsString = celsFld.getText();
String kelvString = kelvFld.getText();
//convert the Strings into double
double fahrVal = Double.parseDouble(fahrFldg);
double celsVal = Double.parseDouble(celsFld);
double kelvVal = Double.parseDouble(kelvFld);
///need a value to hold the input
double input = 0.0;
//the input entered now needs to be calculated
if(event.equals(fahrString))
{
//fahrenheit to kelvin
fahrVal = ((input-32.0)/1.8) + 273.15;
kelvField.setText("" + fahrValue);
}
else if(event.equals(fahrString))
{
//fahrenheit to celsius
celsVal = (5.0/9.0) * (input-32.0);
celsField.setText(""+ celsVal);
}
else if(event.equals(celsString))
{
//celsius to fahrenheit
fahrVal = ((9.0/5.0)*input) + 32.0;
fahrField.setText(""+ fahrVal);
}
else if(event.equals(celsString))
{
//celsius to kelvin
kelvVal = input + 273.15;
celsField.setText(""+ kelvVal);
}
else if(event.equals(kelvString))
{
//kelvin to fahrenheit
fahrVal = ((input - 273) * 1.8 ) + 32.0;
celsField.setText("" + fahrVal);
}
else if (event.equals(kelvFld))
{
//kelvin to celsius
celsVal = input - 273.15;
celsField.setText(Double.toString(celsVal));
}
//clears all conversions when clear button is pressed
if (event.getSource() == clearButton){
celsFld.setText("");
kelvFld.setText("");
fahrFld.setText("");}