簡體   English   中英

如何在JTextField的2個JComboBox中使用值?

[英]How do I use the value in 2 JComboBox's in JTextField?

我正在嘗試做的是創建2個JComboBox和2個JTextField框。 我需要能夠編寫在第一個JComboBox中使用溫度類型(華氏度,攝氏和開爾文)的代碼,並將該第一溫度類型轉換為在第二個JComboBox中選擇的溫度類型。 必須使用在第一個JTextField框中輸入的任何數字(將是所選臨時類型的初始值)並將其轉換為第二個JTextField框中的新溫度類型來完成。 這是我取得的進步...

運行測試時,我在第40行收到NullPointerException ,並且我不知道是否正確格式化了if語句中使用的雙精度值以使新值再次在第2個JTextField框中顯示為String。 在我編寫所有其他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時觸發

當您有兩個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);
        }
    });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM