我一直在研究一個類項目,並且有一個我無法弄清楚的程序。它應該將溫度從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
是否有任何異常跟蹤? –
嗯,我得到它停止鎖定。現在我收到錯誤消息「線程中的異常」AWT-EventQueue-0「java.lang.NullPointerException」 – Jim
這就是我的想法。你能在你的文章中包含完整的痕跡嗎? –