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("");}
4

3 回答 3

1

有几个解决方案适合您。

解决方案 1

你可以在某个地方记住你的旧价值观。在此之后 - 在您的按钮处理程序中,您可以将新值与旧值进行比较。

然而,像这样带有按钮的计算器并不是最好的主意。


解决方案2(在我看来 - 更好)

您可以编写事件,当您更改文本并在该事件处理程序中计算新值时,该事件会在您的文本框中触发。我不是 Java 专家,我无法提供代码示例。

于 2013-08-14T12:56:26.557 回答
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

于 2013-08-14T12:59:59.163 回答
0

You could try something like this. Basically the MyKeyAdapter runs every time you edit one of the field and saves the edited instance. Next you check which field was edited and fire your calculation. The clearButton should be likely handled in separate listener because there is not need to calculate temperatures in this case. Hope you get the idea, and you can progress.

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("");
        }
    }
}
于 2013-08-14T13:59:46.027 回答