2

我想连接该方法returnedConversion,以便ActionListener在用户选择要转换的温度比例后将结果返回给。我意识到代码至少可以说有点脱节,所以请忽略所有注释掉的区域(除非你能指出我应该注意的区域。

如何将该代码从方法连接到代码,returnedConversion以便ActionListener代码正确运行?另外,我是否正确地将JTextField框的输入转换为双精度,然后将其适当地转换回 aString以将其传递回第二个JTextField框?

package temperatureConverter;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class TempConverter extends JFrame {
    private JComboBox firstComboBox;
    private JComboBox secondComboBox;
    private JTextField initialTemp;
    private JTextField convertedTemp;
    private JButton convertButton;
    // private enum TempType { FAHRENHEIT, CELSIUS, KELVIN};
    private static final String[] tempType = { "Fahrenheit", "Celsius",
            "Kelvin" };
    public static final String theInitialTempType = null;
    public static final String theTempTypeToConvertTo = null;
    public static final String theChosenTemp = null;
    public static final String theNewTemp = null;

    public TempConverter() {
        super("Temperature Converter");
        setLayout(new FlowLayout());

        firstComboBox = new JComboBox(tempType);
        firstComboBox.setMaximumRowCount(3);
        firstComboBox.addActionListener(null);
        add(firstComboBox);
        secondComboBox = new JComboBox(tempType);
        secondComboBox.setMaximumRowCount(3);
        secondComboBox.addActionListener(null);
        add(secondComboBox);
        initialTemp = new JTextField("", 10);
        initialTemp.addActionListener(null);
        add(initialTemp);
        convertedTemp = new JTextField("", 10);
        convertedTemp.addActionListener(null);
        add(convertedTemp);
        convertButton = new JButton("Convert");
        convertButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                String applyIt = returnedConversion(initialTemp.getText());
                System.out.println(applyIt);
//              convertedTemp.returnedConversion();
                // ???????????????????????????????????????????
            }
        });
        add(convertButton);
        // String theInitialTempType = (String) firstComboBox.getSelectedItem();
        // String theTempTypeToConvertTo = (String)
        // secondComboBox.getSelectedItem();
        // String theChosenTemp = initialTemp.getSelectedText();
        // String theNewTemp = convertedTemp.getSelectedText();
    }

    // public class textHandler implements ActionListener
    // {
    // public void itemStateChanged (ActionEvent event)
    // {
    // double convertedNumberForTheChosenTemp =
    // Double.parseDouble(theChosenTemp);
    // double convertedNumberForTheNewTemp = Double.parseDouble(theNewTemp);
    // String string1 = "";
    // String string2 = "";
    //
    // if ( theInitialTempType == tempType[0] && theTempTypeToConvertTo ==
    // tempType[1] )
    //
    // convertedNumberForTheNewTemp = (convertedNumberForTheChosenTemp - 32) * 5
    // / 9;
    // String result = String.valueOf(convertedNumberForTheNewTemp);
    // convertedTemp.getSelectedText();
    // }

    // @Override
    // public void actionPerformed (ActionEvent e) {
    //
    // }
    // }
    public String returnedConversion(String toConvert) {
        double convertedNumberForTheChosenTemp = Double.parseDouble(theChosenTemp);
        double convertedNumberForTheNewTemp = Double.parseDouble(theNewTemp);

        if (theInitialTempType == tempType[0] && theTempTypeToConvertTo == tempType[1]) {
            convertedNumberForTheNewTemp = (convertedNumberForTheChosenTemp - 32) * 5 / 9;
            String result = String.valueOf(convertedNumberForTheNewTemp);
            return result;
        } else if (theInitialTempType == tempType[0] && theTempTypeToConvertTo == tempType[2]) {
            convertedNumberForTheChosenTemp = (convertedNumberForTheChosenTemp + 459.67) / 1.8;
            String result = String.valueOf(convertedNumberForTheNewTemp);
            return result;

        } else if (theInitialTempType == tempType[1] && theTempTypeToConvertTo == tempType[0]) {
            convertedNumberForTheChosenTemp = (convertedNumberForTheChosenTemp * 1.8) + 32;
            String result = String.valueOf(convertedNumberForTheNewTemp);
            return result;
        } else if (theInitialTempType == tempType[1] && theTempTypeToConvertTo == tempType[2]) {
            convertedNumberForTheChosenTemp = convertedNumberForTheChosenTemp + 273.15;
            String result = String.valueOf(convertedNumberForTheNewTemp);
            return result;
        } else if (theInitialTempType == tempType[2] && theTempTypeToConvertTo == tempType[0]) {
            convertedNumberForTheChosenTemp = (convertedNumberForTheChosenTemp * 1.8) - 459.67;
            String result = String.valueOf(convertedNumberForTheNewTemp);
            return result;
        } else if (theInitialTempType == tempType[2] && theTempTypeToConvertTo == tempType[1]) {
            convertedNumberForTheChosenTemp -= 273.15;
            String result = String.valueOf(convertedNumberForTheNewTemp);
            return result;
        }

        return null;
    }

    public static void main(String[] args) {
        TempConverter tempTest = new TempConverter();
        tempTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tempTest.setSize(300, 200);
        tempTest.setVisible(true);
    }
}
4

2 回答 2

6

作为第一步,请阅读“为常用事件实现侦听器”。这将使您很好地了解如何使用基本事件处理Swing

如果我理解正确,这是您想要实现的目标:

  • JComboBox用户使用您提供的选项选择转换选项。
  • 用户在您的第一个JTextField调用中输入值initialTemp
  • 用户按下Convert JButton然后您想要捕获该事件,在第一个中隐藏文本JTextField并在第二个中显示转换后的结果JTextField

因此,作为第一步,您要实现一个进行转换的方法,即当用户按下Convert按钮时,将调用此方法,它将从第一个获取值JTextField,执行转换并将其更新为文本值第二个JTextField。您有一个名为 的方法public String returnedConversion(String toConvert),我建议对此进行一些更改:

public void returnedConversion(String initialValue){
    //Step 1. Validate the input
    //Step 2. Convert the value. You write your own logic taking into account the initialValue
    //        and the JComboBox conversion options
    //Step 3. Set the text of the second JTextField to the converted value, using the method convertedTemp.setText(...)
}

现在,您可能希望在调用 时调用此方法Convert JButton。所以,正如你所做的那样,你会希望 anActionListener与它相关联。现在你在那里做什么?好吧,像这样:

convertButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
        returnedConversion(initialTemp.getText());
    }
});

我希望这能给你正确的指导来帮助你编写代码。

另外作为最后一点,您可能需要阅读“Threads and Swing”“Threading with Swing”以了解如何启动您的 Swing 应用程序

于 2012-09-01T07:17:06.180 回答
2

第 49 行:

String applyIt = returnedConversion(toConvert);

在这里,您应该将字符串传递给returnedConversion方法,但您尚未将toConvert变量声明和初始化为字符串。

第 50 行:

convertedTemp.returnedConversion();

convertedTemp是 JTextField 类型。所以你不能returnedConversion()在这里访问未定义的方法。如果您尝试在convertedTempJTextField 中显示文本,您应该使用convertedTemp.setText(applyIt).

于 2012-09-01T06:30:48.160 回答