简体   繁体   中英

Having trouble displaying errors with swing

I am trying to have this application throw an error and display the text but it just keeps showing null. The error needs to show if the person is trying to withdraw more than is in the account. Here is what I have. How would I go about making the text show up when the balance is not high enough? Thanks in advance!

Im not even sure if the errors need to be handled and displayed in the Account class where they are now or the WithdrawListener...

MainFrame class

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;
    }
}

and the Account class that I have checking the account balance

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;
    }
}

I can see you setting the error message but I can't see where you go catch it, so what you can do is:

 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;
    }
}

if the balance is less than the amount you just sent a error msg... and you dont need to do balance = balance, its the same that saying 1 = 1, 1 is already 1...

Hope it help :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM