1

所以我正在创建一个标准银行账户余额为 500 的程序。该程序询问用户是否要提取或存入资金,然后计算他们提取或存入的金额并更新当前余额。为什么它不起作用,我将如何解决它?

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()) {
            }
        }
    }
}
4

2 回答 2

2

您没有在 actionPerformed 方法中使用正确的“if”。你应该使用:

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

代替:

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

isSelected() 不是知道是否单击了 JButton 的正确方法。您必须将点击事件的来源与您的按钮进行比较。

于 2013-07-23T11:55:08.307 回答
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));
    }        
}

您可能还想正确格式化金额,看看这里

这里的许多用户也会争辩说您应该使用BigDecimal而不是double用于财务计算。但由于这是一个简单的应用程序,所以double会很好。

于 2013-07-23T12:01:35.757 回答