java无法使组合框正常工作
我一直在做一个班级项目,有一个项目我搞不懂。它应该将温度从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
# 1 楼答案
我没有足够的代表发表评论,所以我会尝试一下。我知道你有:
但它最初在哪里初始化?因为它是
private
,所以它在同一个类中吗?例如,我没有看到这样的情况:在你的代码中。可能是
comboBoxState()
无法访问它的原因?我想看更多的EngineeringSpecificationInterface.java
。是不是这段代码是从哪里来的# 2 楼答案
除了您在示例中初始化的
resultLabel
字段之外,是否还有一个resultLabel
字段存储为类字段?如果是这样,则构造函数中的局部resultLabel
正在屏蔽它-构造函数使用该名称声明自己的变量,然后对其进行初始化(并重新初始化以添加第二个标签),因此当您到达comboBoxState()
时,类级标签仍然是null
。您需要将第一个resultLabel
变量重命名为其他变量,并保持第二个标签初始化不变