我在使用我的小程序时遇到问题。我有一个文本字段,显示一次从列表框或组合框中选择的一个项目。当选择该项目时,它会在文本字段中显示其价格。然后,我可以选择按一个按钮jbtCart,以便将物品发送到购物车。我遇到问题时,只要按下按钮,该值不会显示购物车中小计标签中的值。使用Java中的列表框和组合框进行事件处理
错误被称为:
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没有被初始化。
为什么要编写一个小程序?如果这是由于规格。由老师,请参考[为什么CS老师应该停止教Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/)。 –
*“我遇到的另一个问题..”*如果您有其他问题,请另外提问。 SO是问答网站,而不是帮助台。 –