我有一个应该使用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。代码仍然相同。
答案 0 :(得分:0)
try{
AmountWithdrawn = Integer.parseInt(NOW);
}catch(Exception ex){
//show a message that the input is wrong
}
这将解决问题