2013-10-19 67 views
0

我一直在研究一個類項目,並且有一個我無法弄清楚的程序。它應該將溫度從F轉換爲C,反之亦然。當我嘗試改變溫度。格式從F到C(F是默認值)在組合框中,程序鎖定。任何人都指向正確的方向?無法使組合框正常工作

// Create and format Temperature Calculator Tab 
private void TempCalcTab(){ 

    // Format panel 
    JPanel tempCalcPanel = new JPanel(); 
    tempCalcPanel.setLayout(null); 
    this.tabbedPane.addTab("Temp Calc", tempCalcPanel); 

    //Create, format and add components to panel 
    JLabel tempLabel = new JLabel("Enter Temperature:"); 
    tempLabel.setSize(115, 20); 
    tempLabel.setLocation(10, 40); 
    tempCalcPanel.add(tempLabel); 
    tempText = new JTextField(); 
    tempText.setSize(120, 20); 
    tempText.setLocation(140, 40); 
    tempText.setText("0"); 
    tempCalcPanel.add(tempText); 
    //****************************************************************** 
    JLabel resultLabel = new JLabel("Result:"); 
    resultLabel.setSize(45, 20); 
    resultLabel.setLocation(10, 80); 
    tempCalcPanel.add(resultLabel); 
    resultLabel = new JLabel("F"); 
    resultLabel.setSize(15, 20); 
    resultLabel.setLocation(280, 80); 
    tempCalcPanel.add(resultLabel); 
    //****************************************************************** 
    resultText = new JTextField(); 
    resultText.setSize(120, 20); 
    resultText.setLocation(140, 80); 
    resultText.setEditable(false); 
    resultText.setText("32"); 
    tempCalcPanel.add(resultText); 
    //****************************************************************** 
    comboBox = new JComboBox(new String[] {"C", "F"}); 
    comboBox.setSize(90, 25); 
    comboBox.setLocation(280, 40); 
    comboBox.setEditable(false); 
    comboBox.addItemListener(new ItemListener(){ 
     public void itemStateChanged(ItemEvent e){ 
      comboBoxState(); 
      } 
     }); 
    tempCalcPanel.add(comboBox); 
    //****************************************************************** 
    JButton convertButton = new JButton("Convert"); 
    convertButton.setSize(150, 25); 
    convertButton.setLocation(35, 120); 
    tempCalcPanel.add(convertButton); 
    convertButton.setMnemonic('C'); 
    convertButton.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      convertTemperature(); 
      } 
     }); 
    //****************************************************************** 
    JButton exitButton = new JButton("Exit"); 
    exitButton.setSize(100, 25); 
    exitButton.setLocation(190, 120); 
    tempCalcPanel.add(exitButton); 
    exitButton.setMnemonic('X'); 
    exitButton.addActionListener(new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      closeProgram(); 
      } 
     }); 
}// End TempCalcTab method 

// Calculating and Formatting Temperature Calculator 

// Formatting comboBox for F or C 
private void comboBoxState(){ 
    if(comboBox.getSelectedItem().toString().equals("C")){ 
     resultLabel.setText("F"); 
    } 
    else{ 
     resultLabel.setText("C"); 
    } 
}// End comboBoxState method 

// Formatting and calculating temperature conversions 
private void convertTemperature(){ 

    // Declare variables 
    double temperature = 0.0; 
    double result = 0.0; 

    // Validating input 
    if(tempText.getText().length() < 1){ 
     tempText.setText("0"); 
    } 

    try{ 
     temperature = Double.parseDouble(tempText.getText()); 
    } 

    catch(Exception ex){ 
     temperature = 0.0; 
    } 

    // Converting to celsius or fahrenheit 
    if(comboBox.getSelectedItem().toString().equals("C")){ 
     result = (temperature * 9/5) + 32; 
    } 

    else{ 
     result = (temperature - 32) * 5/9; 
    } 

    // Format and display results 
    DecimalFormat decimalFormat = new DecimalFormat("##.##"); 
    resultText.setText(decimalFormat.format(result)); 
}// End convert temperature method 
+0

是否有任何異常跟蹤? –

+0

嗯,我得到它停止鎖定。現在我收到錯誤消息「線程中的異常」AWT-EventQueue-0「java.lang.NullPointerException」 – Jim

+0

這就是我的想法。你能在你的文章中包含完整的痕跡嗎? –

回答

0

我沒有足夠的代表評論,所以我會在這個問題上採取措施。我看你有:

comboBox = new JComboBox(new String[] {"C", "F"}); 

但是它最初初始化在哪裏?是因爲它是private嗎?例如,我沒有看到類似的東西:

JComboBox comboBox = new JComboBox(new String[] {"C", "F"}); 

在您的代碼中。可能是comboBoxState()無法訪問它的原因?我想看更多EngineeringSpecificationInterface.java。這是它的代碼片段是從哪裏來的?

+0

'EngineeringSpecificationInterface.java'大約有1000行。這個方法的全局變量是 'private JTextField tempText,resultText; \t private JComboBox comboBox;在構造函數中調用'TempCalcTab' – Jim

+0

也許嘗試在代碼中將'comboBox.getSelectedItem()。toString()'設置爲字符串,然後調用'variable.equals(「 C「)'看看你是否在兩個地方都有錯誤。也許你可以試着找出更多的東西,看看它是否真的是你抓到的。 '783','778'和'280'的確切線是什麼? – Ducksauce88

0

除了您在示例中初始化的類別之外,您還有一個resultLabel存儲爲類字段嗎?如果是的話,你構造函數中的resultLabel掩碼 - 構造函數聲明它自己的變量並初始化它(並重新初始化它以添加第二個標籤),所以當你得到類級標籤時仍然是nullcomboBoxState()。您需要將第一個resultLabel變量重命名爲其他內容,並保持第二個標籤的初始化。

+0

我嘗試重命名該字段,結果相同。這是令人沮喪的,我已經完成了我讀過的所有內容。從書中和所有的董事會。我知道這將是愚蠢的。 – Jim

+0

您是否使用IDE的重構功能進行重命名?這可能會重命名這兩個,重新創建您的問題,但使用不同的變量名稱。如果堆棧跟蹤中的行指向'setText()'方法,那麼你的標籤肯定是'null'的東西。 – Josh

+0

噢,重新閱讀您的評論 - 如果您重命名類字段,您需要查看您的構造函數,並重命名第二個'resultLabel'初始值設定項以匹配它 - 從'resultLabel = new JLabel(「F」)開始; ' – Josh