我一直在做一个课堂项目,并且有一个我无法弄清楚的程序。它应该将温度从 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