문제

I need to do a conversion of temperature. When I input one value into either fahrenheit, celsius or kelvin, and press calculate, it will calculate the other 2 values. There are only two buttons, calculate and clear. How would you input one value and get them to calculate the other two values when I press the calculate button?

 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("");}
도움이 되었습니까?

해결책 2

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

다른 팁

There are few solutions for you.

Solution 1

You can remember your old values somewhere. After this - in your button handler you can compare new values with old values.

However calculator like this with button is not best idea.


Solution 2 (in my opinion - better)

You can write event, that fires on your textboxes when you change text and calculate new values in that event handler. Im not Java specialist, I can't provide code example.

Your conditions have 2 major problems.

First:

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.

Second:

if(event.equals(fahrString)) {

will always be false, because event is an ActionEvent and fahrString is a String.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top