无法使用swing显示错误

时间:2013-11-22 01:31:02

标签: java swing

我试图让这个应用程序抛出一个错误并显示文本,但它只是一直显示为null。如果此人试图提取的金额超过帐户中的金额,则需要显示错误。这就是我所拥有的。当余额不够高时,我如何才能显示文字?提前谢谢!

我甚至不确定是否需要在现在的Account类或WithdrawListener中处理和显示错误......

MainFrame类

package appgui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class MainFrame extends JFrame implements AccountDisplay {
    private BankAccount account;
    private JLabel label;
    private JLabel errorLabel;
    private JTextField amountField;

    private static final int FRAME_HEIGHT = 600;
    private static final int FRAME_WIDTH = 400;

    public MainFrame(String t, BankAccount anAccount) {
        super(t);

        account = anAccount;

        // Label that displays the result
        label = new JLabel("Balance: " + account.getBalance());
        label.setFont(new Font("Arial", Font.BOLD, 22));
        label.setForeground(Color.RED);

        errorLabel = new JLabel("Error: " + account.getErrors());

        // Create the label, text field, and button for entering amount
        JLabel amountLabel = new JLabel("Amount: ");
        amountField = new JTextField(7);

        // Create the Control Panel that holds all components
        JPanel controlPanel = new JPanel();
        controlPanel.add(amountLabel);
        controlPanel.add(amountField);
        controlPanel.add(createWithdrawButton());
        controlPanel.add(createDepositeButton());
        controlPanel.add(label);
        controlPanel.add(errorLabel);

        add(controlPanel);
        setSize(FRAME_HEIGHT, FRAME_WIDTH);
        //setLayout(new GridBagLayout());
    }

    private JButton createDepositeButton() {
        JButton depositButton = new JButton("Deposit");

        ActionListener depositLstener = new DepositListener(this);
        depositButton.addActionListener(depositLstener);
        return depositButton;
    }

    private JButton createWithdrawButton() {
        JButton withdrawButton = new JButton("Withdraw");

        ActionListener withdrawListener = new WithdrawListener(this);
        withdrawButton.addActionListener(withdrawListener);
        return withdrawButton;
    }

    public String getAmount() {
        return this.amountField.getText();
    }

    public BankAccount getAccount() {
        return this.account;
    }

    public JLabel getLabel() {
        return this.label;
    }
}

以及我检查帐户余额的帐户类

package appgui;

public class BankAccount {
    private double balance;
    private String error;

    public BankAccount(int b) {
        balance = b;
    }

    public void deposit(double amount) {
        balance = balance + amount;
    }

    public void withdraw(double amount) {
        if(balance - amount < 0) {
            balance = balance;
            setErrors("Can't withdraw that amount, account balance is too low.");
        } else {
            balance = balance - amount;
        }
    }

    public double getBalance() {
        return this.balance;
    }

    public String setErrors(String err) {
        this.error = err;
        return err;
    }

    public String getErrors() {
        return this.error;
    }
}

1 个答案:

答案 0 :(得分:0)

我可以看到你设置错误信息,但我看不到你去哪里捕捉它,所以你可以做的是:

 public void withdraw(double amount) {
    if(balance - amount < 0) {
        JOptionPane.showMessageDialog(null, "Can't withdraw that amount, account balance is too low.", "Error", JOptionPane.ERROR_MESSAGE);
    } else {
        balance = balance - amount;
    }
}

如果余额低于您刚发送错误消息的数量... 而且你不需要做平衡=平衡,就像说1 = 1,1已经是1 ...

希望有所帮助:)