我需要进行温度转换。当我将一个值输入华氏度,摄氏度或开尔文时,按下计算,它将计算其他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("");}
答案 0 :(得分:1)
您的解决方案很少。
解决方案1
你可以在某处记住你的旧价值观。在此之后 - 在您的按钮处理程序中,您可以将新值与旧值进行比较。
然而,按钮这样的计算器不是最好的主意。
解决方案2(在我看来 - 更好)
您可以编写事件,当您更改文本并在该事件处理程序中计算新值时,该事件将触发文本框。我不是Java专家,我不能提供代码示例。
答案 1 :(得分:0)
您的情况有两个主要问题。
首先:
if(event.equals(fahrString))
{
// some code
}
else if(event.equals(fahrString))
{
// this will be never be executed, because if the condition
// were true, then the if block would be executed and this skipped
}
// the same for all of your other conditions.
第二
if(event.equals(fahrString)) {
始终为false
,因为event
是ActionEvent
而fahrString
是String
。
答案 2 :(得分:0)
你可以尝试这样的事情。基本上,每次编辑其中一个字段时,MyKeyAdapter都会运行并保存已编辑的实例。接下来,检查编辑了哪个字段并激活计算。 clearButton应该可以在单独的监听器中处理,因为在这种情况下不需要计算温度。希望你能得到这个想法,并且你可以继续前进。
private void initStuff () {
calculateButton.addActionListener(new ButtonActionEvent());
clearButton.addActionListener(new ButtonActionEvent());
fahrFld.addKeyListener(new MyKeyAdapter());
celsFld.addKeyListener(new MyKeyAdapter());
kelvFld.addKeyListener(new MyKeyAdapter());
}
private JTextField lastModified;
private class MyKeyAdapter extends KeyAdapter {
@Override
public void keyTyped(KeyEvent e) {
lastModified = (JTextField)e.getSource();
}
}
private class ButtonActionEvent 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;
if(lastModified == fahrFld)
{
double input = Double.parseDouble(fahrFld.getText());
//fahrenheit to kelvin
double kelvVal = ((input-32.0)/1.8) + 273.15;
kelvFld.setText("" + kelvVal);
//fahrenheit to celsius
double celsVal = (5.0/9.0) * (input-32.0);
celsFld.setText(""+ celsVal);
}
else if(lastModified == celsFld)
{
double input = Double.parseDouble(celsFld.getText());
//celsius to fahrenheit
double fahrVal = ((9.0/5.0)*input) + 32.0;
fahrFld.setText(""+ fahrVal);
//celsius to kelvin
double kelvVal = input + 273.15;
kelvFld.setText(""+ kelvVal);
}
else if(lastModified == kelvFld)
{
double input = Double.parseDouble(kelvFld.getText());
//kelvin to fahrenheit
double fahrVal = ((input - 273) * 1.8 ) + 32.0;
fahrFld.setText("" + fahrVal);
//kelvin to celsius
double celsVal = input - 273.15;
celsFld.setText(Double.toString(celsVal));
} else {
return;
}
//clears all conversions when clear button is pressed
if (event.getSource() == clearButton){
celsFld.setText("");
kelvFld.setText("");
fahrFld.setText("");
}
}
}