2013-07-23 82 views
1

因此,我創建了一個標準銀行賬戶餘額爲500的程序。該程序詢問用戶是否想要提取或存款,然後計算他們退出或存入的金額並更新當前餘額。爲什麼它不起作用,我將如何解決它?如何輸入,計算和更新JLabel?

public class MyFrame extends JFrame { 

    private JPanel panel; 
    private JLabel wordsLabel; 
    private JLabel balanceLabel; 
    private JLabel choiceLabel; 
    private JTextField transactionAmount; 
    private JButton depositButton; 
    private JButton withdrawButton; 
    private double balance; 

    public MyFrame() { 
     final int FIELD_WIDTH = 10; 
     balance = 500; 
     panel = new JPanel(); 
     wordsLabel = new JLabel(); 
     balanceLabel = new JLabel(); 
     choiceLabel = new JLabel(); 
     transactionAmount = new JTextField(FIELD_WIDTH); 
     JPanel buttonPanel = new JPanel(); 
     ButtonGroup myGroup = new ButtonGroup(); 
     //panel.setLayout(new BorderLayout()); 
     depositButton = new JButton("Deposit"); 
     withdrawButton = new JButton("Withdraw"); 
     transactionAmount.setText("0"); 
     wordsLabel.setText("Welcome to Wes Banco! Your current balance is: "); 
     balanceLabel.setText("500"); 
     choiceLabel.setText("How much would you like to deposit/withdraw?"); 
     panel.add(wordsLabel); 
     panel.add(balanceLabel); 
     panel.add(choiceLabel); 
     panel.add(transactionAmount); 
     myGroup.add(depositButton); 
     myGroup.add(withdrawButton); 
     buttonPanel.add(depositButton); 
     buttonPanel.add(withdrawButton); 
     panel.add(depositButton); 

     ButtonListener myListener = new ButtonListener(); 
     depositButton.addActionListener(myListener); 
     withdrawButton.addActionListener(myListener); 

     panel.add(buttonPanel); 
     this.add(panel); 
    } 

    class ButtonListener implements ActionListener { 

     public void actionPerformed(ActionEvent event) { 
      double amount = Double.parseDouble(transactionAmount.getText()); 
      if (amount == 0) { 
       JOptionPane.showMessageDialog(null, "Enter an amount"); 
      } 
      if (depositButton.isSelected()) { 
       balanceLabel.setText("" + 500 + amount); 
       JOptionPane.showMessageDialog(null, 
        "You have deposited: " + amount); 
      } 
      if (withdrawButton.isSelected()) { 
      } 
     } 
    } 
} 
+0

此外,考慮使用[BigDecimal](http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html)進行與貨幣相關的操作,而不是使用'double'。 –

回答

2

您在actionPerformed方法中沒有使用正確的「if」。您應該使用:的

if (event.getSource() == depositButton) { 
    //recalculate 
} 

代替:

if (depositButton.isSelected()) { 
     //recalculate 
} 

isSelected()不知道,如果一個JButton被點擊了正確的方法。您必須將點擊事件的來源與您的按鈕進行比較。

2

有幾件事情出錯了。

  1. 您使用錯誤的方法來確定哪個按鈕被點擊(如mael指出)。
  2. 您不更新balance變量(因此多個存款/提款將不起作用)。
  3. 您使用字符串計算新餘額。 "" + 500 + amount使用字符串連接,而不是加法。

你需要的東西,如:

public void actionPerformed(ActionEvent event) { 
    double amount = Double.parseDouble(transactionAmount.getText()); 
    if (amount == 0) { 
     JOptionPane.showMessageDialog(null, "Enter an amount"); 
    } else { 
     if (event.getSource() == depositButton) { 
      JOptionPane.showMessageDialog(null, 
       "You have deposited: " + amount); 
      balance += amount; 
     } else if (event.getSource() == withdrawButton) { 
      if (balance < amount) { 
       JOptionPane.showMessageDialog(null, 
        "You cannot withdraw more than your balance."); 
      } else { 
       JOptionPane.showMessageDialog(null, 
        "You have withdrawn: " + amount); 
       balance -= amount; 
      } 
     } 
     balanceLabel.setText(String.valueOf(balance)); 
    }   
} 

您可能還需要正確地格式化量,看看here

此處的許多用戶也會爭辯說,您應該使用BigDecimal而不是double進行財務計算。但由於這是一個簡單的應用程序,double會很好。

+0

我剛剛看到了您提供的有關BigDecimal使用情況的信息。已經投了票:-) –

+0

完美的作品!萬分感謝! –

+0

如果餘額<0,它不會允許我添加if語句。我打算這樣做,如果餘額低於0,那麼會出現一條消息,表示沒有足夠的錢該交易,並迫使他們重新進入。 –