2

我试图做的是创建 2 个 JComboBox 和 2 个 JTextField 框。我需要能够编写在第一个 JComboBox 中使用温度类型(华氏、摄氏和开尔文)的代码,并将第一个温度类型转换为在第二个 JComboBox 中选择的任何温度类型。这必须通过使用在第一个 JTextField 框中输入的任何数字(这将是所选温度类型的初始值)并在第二个 JTextField 框中转换为新的温度类型来完成。这是我进步的程度...

当我运行我的测试时,我在第 40 行得到一个NullPointerException,我不知道我是否正确格式化了 if 语句中使用的双精度,以使新值在第二个 JTextField 框中再次显示为字符串。在我编写所有其他 if 语句来处理所有其他场景之前,我正在寻找一些关于我到目前为止所做的事情是否正确的指针。

package temperatureConverter;


import java.awt.FlowLayout;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JComboBox;
import javax.swing.JTextField;

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

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

        firstComboBox = new JComboBox(tempType);
        firstComboBox.setMaximumRowCount(3);
        firstComboBox.addItemListener(null);
        add(firstComboBox);
        secondComboBox = new JComboBox(tempType);
        secondComboBox.setMaximumRowCount(3);
        secondComboBox.addItemListener(null);
        add(secondComboBox);
        initialTemp = new JTextField ("", 10);
        initialTemp.addActionListener(null);
        add(initialTemp);
        convertedTemp = new JTextField ("", 10);
        convertedTemp.addActionListener(null);
        add(convertedTemp);
    }
    String theInitialTempType = (String) firstComboBox.getSelectedItem();
    String theTempTypeToConvertTo = (String) secondComboBox.getSelectedItem();
    String theChosenTemp = initialTemp.getSelectedText();
    String theNewTemp = convertedTemp.getSelectedText();

    private class textHandler implements ItemListener
    {
        public void itemStateChanged (ItemEvent 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);
            }
        }
    }
}
4

2 回答 2

2

由于您有 的两个实例JComboBox,因此此示例显示了第一个组合中的选择如何更改第二个组合中显示的内容。例如,Fahrenheit在第一个组合中选择会将第二个组合的模型更改为仅显示CelsiusKelvin。等等。

附录:正如@Andrew 建议的那样,您必须将所有四个实例变量的初始化移动到构造函数中。这是main()我用来测试您的代码的:

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            TempConverter f = new TempConverter();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.pack();
            f.setVisible(true);
        }
    });
}
于 2012-09-01T02:05:46.330 回答
2
String theInitialTempType = (String) firstComboBox.getSelectedItem();

此代码行位于创建字段的构造函数之外。属性在类的其他方法中使用,因此声明String theAttribute需要在构造函数之外。

另一方面,实例的创建/初始化需要在其他字段创建之后进行,所以在构造函数的最后,theAttribute = anotherAttribute.getSelectedText();

但即使这样也不正确。在那个阶段,这些字段是空的,因此尝试从它们计算结果是没有意义的。计算应由最终用户控制,并按操作完成。调查ActionListener- 它可以添加到字段中并会触发Enter

于 2012-09-01T01:43:47.353 回答