java - 如何在 JTextField 中使用 2 个 JComboBox 中的值?

标签 java swing nullpointerexception jcombobox

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

当我运行测试时,我在第 40 行收到 NullPointerException ,并且我不知道我是否已正确格式化 if 语句中使用的 double 以使新值再次显示为第二个 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);
            }
        }
    }
}

最佳答案

String theInitialTempType = (String) firstComboBox.getSelectedItem();

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

另一方面,实例的创建/初始化需要在创建其他字段之后完成,因此在构造函数的末尾,theAttribute = anotherAttribute.getSelectedText();

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

时触发

关于java - 如何在 JTextField 中使用 2 个 JComboBox 中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12224229/

相关文章:

java - 获取Android中已添加的网络列表

java - 解析字符串中的状态模式用法

java - 在 JPanel 上显示 JFreeChart 的缩略图

Java:数组:检查行元素

java - 为什么这个 for 循环不起作用?

java - JFileChooser 保存图像没有添加文件类型

java - NimbusLookAndFeel 无法解析为变量

android - API 15 上的 SupportAppCompat-v7 库中的 NullPointerException

java - 我的 NullPointerException 的原因是什么?

java - 初始化非静态 JPanel 时偶尔出现 nullpointerexception