2014-05-07 13 views
0

我在使用我的小程序时遇到问题。我有一个文本字段,显示一次从列表框或组合框中选择的一个项目。当选择该项目时,它会在文本字段中显示其价格。然后,我可以选择按一个按钮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没有被初始化。

+0

为什么要编写一个小程序?如果这是由于规格。由老师,请参考[为什么CS老师应该停止教Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/)。 –

+0

*“我遇到的另一个问题..”*如果您有其他问题,请另外提问。 SO是问答网站,而不是帮助台。 –

回答

3

您的问题是你要分析包含货币符号,"$"一个字符串,而事实上的错误是告诉你正是这一点:

Exception in thread "AWT-EventQueue-1" java.lang.NumberFormatException: 
     For input string: "$5.75" 
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) 

我反而使用的NumberFormat,货币而不是您的String.format(...)位。它的目的是使用当前区域的货币符号并在解析时接受它。

NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(); 
currencyFormat.setMinimumIntegerDigits(4); // if you want a minimum int digit count 

System.out.println(currencyFormat.format(120324.444)); 
System.out.println(currencyFormat.format(12.40404)); 

这也没有任何意义:

double itemSubtotal = itemPrice + itemSubtotal; 

你宣布在该行的变量,为什么你会尝试把它赋值语句时的右侧它不具有任何价值?

更好的办法是在方法中声明itemSubtotal,将其初始值设置为0.0,然后使用它,可能在for循环中使用它(或者使用更紧凑的+=运算符),如果您想要在一些列表中添加所有项目。

+0

非常感谢。我将itemSubtotal声明为一个类变量,并将其初始化为0.0,然后在我的方法中调用它。有效! – Stephane