【发布时间】:2014-05-07 00:30:07
【问题描述】:
我的小程序有问题。我有一个文本字段,显示一次从列表框或组合框中选择的一个项目。选择该项目后,它会在文本字段中显示其价格。然后,我可以选择按下按钮 jbtCart,以便将商品发送到购物车。我遇到了问题,每当我按下按钮时,该值都不会在购物车的小计标签中显示该值。
错误被调用:
Exception in thread "AWT-EventQueue-1" java.lang.NumberFormatException: For input string: "$5.75"
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at LanceSubs$6.actionPerformed(LanceSubs.java:380)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
这里是相关的处理程序。
//WEST PANEL
//Select Menu items
jlstsandwiches.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e){
/***int[] indices = jlstsandwiches.getSelectedIndices();
int i;
for(i = 0; i < indices.length; i++){
jtfItemPrice.setText(sandwichPrices[indices[i]].toString());
}*/
int index = jlstsandwiches.getSelectedIndex();
jtfItemPrice.setText(String.format("$%4.2f",
sandwichPrices[index]));
}
});
jlstdrinks.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e){
/***int[] indices = jlstsandwiches.getSelectedIndices();
int i;
for(i = 0; i < indices.length; i++){
jtfItemPrice.setText(sandwichPrices[indices[i]].toString());
}*/
int index = jlstdrinks.getSelectedIndex();
jtfItemPrice.setText(String.format("$%4.2f",
drinksPrices[index]));
}
});
//NORTH PANEL
//Select combo box items for special sandwiches and drinks
jcbospecials.addItemListener(new ItemListener(){
@Override
public void itemStateChanged(ItemEvent e){
int index = jcbospecials.getSelectedIndex();
jtfItemPrice.setText(String.format("$%4.2f",
specialSandwichPrices[index]));
}
});
jcbodrinks.addItemListener(new ItemListener(){
@Override
public void itemStateChanged(ItemEvent e){
int index = jcbodrinks.getSelectedIndex();
jtfItemPrice.setText(String.format("$%4.2f",
specialDrinksPrices[index]));
}
});
//CENTER PANEL
//Display itemPrice and itemSubtotal in Your Subtotal sub-menu
jbtCart.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e){
double itemPrice = Double.parseDouble(jtfItemPrice.getText());
double itemSubtotal = itemPrice + itemSubtotal;
jlblItemPrice2.setText(String.format("$%4.2f", itemPrice));
jlblSubtotal2.setText(String.format("$%4.2f", itemSubtotal));
}
});
我在 CENTER 面板中遇到的另一个问题是,我无法将商品价格添加到小计中。运行此代码时,我收到一条错误消息,提示 itemSubtotal 未初始化。
【问题讨论】:
-
为什么要编写小程序?如果是由于规范。老师请转至Why CS teachers should stop teaching Java applets。
-
“我遇到的另一个问题..” 如果您还有其他问题,请提出单独的问题。 SO 是一个问答网站,而不是帮助台。
标签: java swing number-formatting numberformatexception