0

我有一个应该使用 GUI 模拟 ATM 机的程序。由于我只有一周的编程经验,我知道我会遇到很多错误,而且确实如此。这就是我的问题开始的地方。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class WithdrawClass implements ActionListener
{
private JTextField AmountField;
private JFrame WithdrawFrame;
private int AmountWithdrawn = 0;
private String NOW;

public void WithdrawClass()
{
    WithdrawFrame = new JFrame("Withdraw");

    JPanel TextPanel = new JPanel();
    JPanel BTPanel   = new JPanel();
    JPanel UniterPanel = new JPanel();

    JLabel Texts = new JLabel("Please Enter Desired Amount: ");

    AmountField = new JTextField(20);

    JButton SubmitBT = new JButton("Enter");
    SubmitBT.addActionListener(this);

    TextPanel.add(Texts);
    TextPanel.add(AmountField);
    BTPanel.add(SubmitBT);
    UniterPanel.setLayout(new GridLayout(2,1));
    UniterPanel.add(TextPanel);
    UniterPanel.add(BTPanel);

    WithdrawFrame.setContentPane(UniterPanel);
    WithdrawFrame.setSize(360,180);
    WithdrawFrame.pack();
    WithdrawFrame.show();
    WithdrawFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    WithdrawFrame.setResizable(false);

    NOW = AmountField.getText();
    AmountWithdrawn = Integer.parseInt(NOW);
}

public void actionPerformed(ActionEvent e)
{        
    if(e.getActionCommand().equals("Enter"))
    {
        WithdrawFrame.hide();
        WithdrawCore Goer = new WithdrawCore();
        Goer.WithdrawCore(AmountWithdrawn);
    }
}
}

当我尝试编译整个东西时,它没有语法错误,但是当我尝试运行它时,它有一个异常。它说它周围有一个空字符串错误

AmountWithdrawn = Integer.parseInt(NOW);

我似乎无法找到解决这个问题的方法。我主要尝试了 JFormattedTextField 但它没有用。如果有人可以为我提供解决方案,我将不胜感激。

编辑

它不再是空字符串了。这是一个 NumberFormatException。仍然是同一行代码。

4

1 回答 1

0
try{
   AmountWithdrawn = Integer.parseInt(NOW);
}catch(Exception ex){
   //show a message that the input is wrong
}

这将解决问题

于 2013-10-20T17:13:36.887 回答