2013-10-20 44 views
0

我有一個應用程序可以模擬使用GUI的ATM機。由於我只有一週的編程經驗,我知道我會遇到很多錯誤,而且確實如此。這是我的問題開始的地方。JTextField爲整數空字符串錯誤

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
try{ 
    AmountWithdrawn = Integer.parseInt(NOW); 
}catch(Exception ex){ 
    //show a message that the input is wrong 
} 

這樣便解決了問題

相關問題