使用Java中的列表框和组合框进行事件处理

时间:2014-05-07 00:30:08

标签: java swing number-formatting numberformatexception

我的applet出了问题。我有一个文本字段,显示从列表框或组合框一次选择的一个项目。选择项目后,它会在文本字段中显示其价格。然后,我可以选择按下按钮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未初始化。

1 个答案:

答案 0 :(得分: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循环中使用它(或者使用更紧凑的{ {1}}运营商)如果您想在某个列表中添加所有项目。