使用JFrame时的NumberFormatException

时间:2014-10-14 10:21:26

标签: java jframe actionlistener jtextfield numberformatexception

代码应该获得订单及其价格,并将它们保存在各自的ArrayLists中。

public class SetMenu0{

    private double price;
    private int size;
    private String output;
    private String priceOutput;
    String next;
    JTextField orderIn;
    JTextField priceIn;

    private JFrame orderInput;

    JPanel txtFldPanel;
    JPanel btnPanel;

    ArrayList orderList = new ArrayList<String>();
    ArrayList priceList = new ArrayList<Double>();

    public SetMenu0()
    {
        orderInput = new JFrame();
        orderInput.setTitle("Input Order and Price");
        orderInput.setSize(200,350);
        orderInput.getContentPane();
        orderInput.setLayout(new BorderLayout());
        orderInput.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel txtFldPanel = new JPanel();
        orderIn = new JTextField(10);
        priceIn = new JTextField(5);
        txtFldPanel.add(orderIn);
        txtFldPanel.add(priceIn);

        JPanel btnPanel = new JPanel();
        JButton addBtn = new JButton("ADD");
        btnPanel.add(addBtn);
        addBtn.addActionListener(new ButtonListener());
        addBtn.setActionCommand("add");
        JButton fnshBtn = new JButton("FINISH");
        btnPanel.add(fnshBtn);
        addBtn.addActionListener(new ButtonListener());
        fnshBtn.setActionCommand("fnsh");

        size = orderList.size();

        Container cont = orderInput.getContentPane();
        cont.add(txtFldPanel,BorderLayout.NORTH);
        cont.add(btnPanel,BorderLayout.SOUTH);

        orderInput.pack();
    }

    private class ButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            String actionEnter = e.getActionCommand();
            if(actionEnter.equals("add"))
            {
                orderList.add(orderIn.getText());
                priceList.add(Double.parseDouble(priceIn.getText()));
                orderIn.setText("");
                priceIn.setText("");
            }
            else if(actionEnter.equals("fnsh"))
            {
                orderInput.dispose();
            }
        }
    }

    public JFrame getFrame()
    {
        return orderInput;
    }

    public ArrayList getOrd()
    {
        return orderList;
    }

    public ArrayList getPri()
    {
        return priceList;
    }

    public int getSize()
    {
        return size;
    }

}

当我按下ADD按钮时,它会显示NumberFormatException。为什么是这样?我可以从main添加其余代码,但为什么不保存在ArrayList中?

这是错误:     java.lang.NumberFormatException:对于输入字符串:&#34;&#34;     at java.lang.NumberFormatException.forInputString(Unknown Source)     在java.lang.Integer.parseInt(未知来源)     在java.lang.Integer.parseInt(未知来源)     at com.howtodoinjava.demo.poi.SetMenu0 $ ButtonListener.actionPerformed(SetMenu0.java:84)     在javax.swing.AbstractButton.fireActionPerformed(未知来源)     at javax.swing.AbstractButton $ Handler.actionPerformed(Unknown Source)     在javax.swing.DefaultButtonModel.fireActionPerformed(未知来源)     在javax.swing.DefaultButtonModel.setPressed(未知来源)     在javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知来源)     at java.awt.Component.processMouseEvent(Unknown Source)     在javax.swing.JComponent.processMouseEvent(未知来源)     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.Window.dispatchEventImpl(Unknown Source)     at java.awt.Component.dispatchEvent(Unknown Source)     at java.awt.EventQueue.dispatchEventImpl(Unknown Source)     在java.awt.EventQueue.access $ 400(未知来源)     在java.awt.EventQueue $ 3.run(未知来源)     在java.awt.EventQueue $ 3.run(未知来源)     at java.security.AccessController.doPrivileged(Native Method)     at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)     at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(Unknown Source)     在java.awt.EventQueue $ 4.run(未知来源)     在java.awt.EventQueue $ 4.run(未知来源)     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)     在java.awt.EventDispatchThread.run(未知来源)

2 个答案:

答案 0 :(得分:1)

这可能是由于这一行:

priceList.add(Double.parseDouble(priceIn.getText()));

在尝试解析文本之前,您需要确保priceIn文本字段包含数字。

答案 1 :(得分:0)

你必须使用(在按钮上添加点击)

if(!(priceIn.isEmpty || orderIn.isEmpty)){
     //Do your adding
}

此代码检查JTextFiels中是否有某些内容