private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//9
txtTot.setText(jComboBox1.getSelectedItem().toString());
tot= Double.parseDouble(txtTot.getText());
CMB= (Double)jComboBox1.getSelectedItem();
Total2=tot+CMB;
txtTot.setText(Double.toString(Total2));
}
从ComboBox
我设置jtextfield
,第二行我从string
获取textfield
并尝试将其转换为{double
1}}。
以下错误:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Double
at test.Test.jComboBox1ActionPerformed(Test.java:392)
at test.Test.access$600(Test.java:18)
at test.Test$7.actionPerformed(Test.java:134)
at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1258)
at javax.swing.JComboBox.setSelectedItem(JComboBox.java:586)
at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:622)
答案 0 :(得分:2)
对我而言,似乎您正在从组合框中选择一个值,然后尝试将它们组合在一起以创建一个总计,每次从组合框中选择一个新值时都会更新总数。
这似乎对我有用,注意我已经走了第一线,因为它干扰了现有的总数。
tot= Double.parseDouble(txtTot.getText());
CMB = Double.parseDouble((String) jComboBox1.getSelectedItem());
Total2=tot+CMB;
txtTot.setText(Double.toString(Total2));
答案 1 :(得分:-1)
尝试将txtTot.getText()打印到控制台并检查它是否采用正确的十进制格式。格式错误可能是错误的原因。