无法使ComboBox正常工作

时间:2013-10-19 00:56:49

标签: java swing jcombobox

我一直致力于一个课程项目,并且有一个我无法弄清楚的程序。它应该将温度从F转换为C,反之亦然。当我试图改变温度。在comboBox中从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

2 个答案:

答案 0 :(得分:0)

我没有足够的代表发表评论所以我会对此有所了解。我看到你了:

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

但它最初在哪里初始化?它是private,因为它在同一个类中吗?例如,我没有看到类似的东西:

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

在你的代码中。可能是comboBoxState()无法访问它的原因?我希望看到更多EngineeringSpecificationInterface.java。那是代码来自这个片段吗?

答案 1 :(得分:0)

除了您在示例中初始化的字段外,您是否将resultLabel存储为类字段?如果是这样,构造函数中的本地resultLabel正在屏蔽它 - 构造函数使用该名称声明自己的变量然后初始化它(并重新初始化它以添加第二个标签),因此类级别标签仍然是到达nullcomboBoxState()。您需要将第一个resultLabel变量重命名为其他变量,并保持第二个标签初始化。

相关问题