我正在做一个银行项目,遇到了问题。我有一个扩展到DepositwindowJDialog类和一个扩展到AccountwindowJFrame类。我的问题是如何通过在Accountwindow类中添加以前的余额和存款金额来更新我的Depositwindow余额,并在Accountwindow余额中显示它?

这是我的Accountwindow

    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.*;
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowEvent;
    import java.awt.event.WindowFocusListener;

    public class AccountWindow extends JFrame {

    private final JButton jbtDeposit;
    private final JButton jbtExit;
    private final Account acct;

    public AccountWindow(Account acct) {
        this.acct = acct;

        // Create panel p1 for the buttons and set GridLayout
        // Set BorderLayout with horizontal gap 5 and vertical gap 10
        setLayout(new BorderLayout(5, 10));

        JPanel pnlButton = new JPanel();

        pnlButton.setBorder(new TitledBorder("Actions"));

        pnlButton.setLayout(new FlowLayout(FlowLayout.RIGHT, 1, 1));

        jbtDeposit = new JButton("Deposit");
        jbtExit = new JButton("Exit");
        pnlButton.add(new JButton("Balance"));
        pnlButton.add(jbtDeposit);
        pnlButton.add(new JButton("Withdraw"));
        pnlButton.add(new JButton("Apply Charges"));
        pnlButton.add(jbtExit);

        this.add(pnlButton, BorderLayout.SOUTH);

        // Create panel p2 to hold a text field and p1
        JPanel p2 = new JPanel(new GridLayout(2, 2));
        p2.setBorder(new TitledBorder("Account"));
        p2.add(new JLabel("Account Number"));
        p2.add(new JLabel(acct.getAcctNum()));
        p2.add(new JLabel("Balance"));
        p2.add(new JLabel(String.format("%.2f",acct.getBalance())));
        // add contents into the frame
        add(p2, BorderLayout.CENTER);

        DepositListenerClass depositListener = new DepositListenerClass();
        jbtDeposit.addActionListener(depositListener);

        this.addWindowFocusListener(new WindowListenerClass());

        jbtExit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                closeWindow();
            }
        });

    }

    //default the focus to the Name field when the window is first displayed.

    private class WindowListenerClass implements WindowFocusListener {

        @Override
        public void windowGainedFocus(WindowEvent e) {
            jbtExit.requestFocusInWindow();
        }

        @Override
        public void windowLostFocus(WindowEvent e) {
        }
    }
    /*
     * Fire a window closing window event so that the calling window can
     * refresh.
     */

    private void closeWindow() {
        System.out.println("exiting");
        WindowEvent wev = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
        Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
    }

    class DepositListenerClass implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            DepositWindow frame = new DepositWindow();
            frame.setTitle("Deposit Window");
            //frame.setSize(400, 250);
            frame.setModal(true);
            frame.pack();
            frame.setLocationRelativeTo(null); // Center the frame
            frame.setVisible(true);
            double amount = frame.getAmount();
        }
    }
}


这是我的DepositWindow

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.BorderLayout;
import java.awt.event.*;

public class DepositWindow extends JDialog
{

    private JButton jbtOk;
    private JButton jbtCancel;
    private JLabel  jlbAccountNumber;
    private JTextField jtfAmount;

    public DepositWindow() {
        setModal(true);
        // Create panel p1 for the buttons and set GridLayout
        // Set BorderLayout with horizontal gap 5 and vertical gap 10
        setLayout(new BorderLayout(5, 10));
        JPanel p1 = new JPanel();
        p1.setBorder( new TitledBorder("Actions"));
        p1.setLayout(new FlowLayout(FlowLayout.RIGHT, 1, 1));
        jbtOk = new JButton("OK");
        jbtCancel = new JButton("Cancel");
        p1.add(jbtOk);
        p1.add(jbtCancel);

        this.add(p1,BorderLayout.SOUTH);

        JPanel p2 = new JPanel(new GridLayout(2,2));
        p2.setBorder( new TitledBorder("Account"));
        p2.add(new JLabel("Account Number"));
        jlbAccountNumber = new JLabel("*****-56");
        p2.add(jlbAccountNumber);
        p2.add(new JLabel("Amount"));
        jtfAmount = new JTextField(10);
        p2.add(jtfAmount);

        // add contents into the frame
        add(p2, BorderLayout.CENTER);

        jbtOk.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                setVisible(false);
            }
        });
  }

  public double getAmount(){
      return Double.parseDouble(jtfAmount.getText());
    }

  /** Main method */
  public static void main(String[] args) {
    DepositWindow frame = new DepositWindow();
    frame.setTitle("Deposit Window");
    frame.pack();
    frame.setLocationRelativeTo(null); // Center the frame
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
 }


帐户类别:





import java.util.ArrayList;

public class Account
{
    private String acctNum;
    protected double balance;
    private String name; //customer name
    private ArrayList<Transaction> transactions;
    private String password;


    @Override
    public String toString() {
        return this.getClass().getName() +
            "[acctNum="+acctNum+
            ",balance="+balance+
            ",name="+name+"]";
    }

    @Override
    public boolean equals(Object o) {
        if (o instanceof Account) {
            return (this.acctNum.equals(((Account)o).acctNum));
        }
        return false;
    }

    public Account() {
        transactions = new ArrayList<Transaction>();
    };

    public Account(String acctNum, double balance) {
        this();
        setAcctNum(acctNum);
        setBalance(balance);
    }

    public Account(String name, String acctNum, double balance) {
        this();
        setAcctNum(acctNum);
        setBalance(balance);
        setName(name);
    }

    public Account(String name, String password, String acctNum, double balance) {
        this();
        setPassword(password);
        setAcctNum(acctNum);
        setBalance(balance);
        setName(name);
    }

    public String getAcctNum() { return acctNum; }
    public double getBalance() { return balance; }
    public void setAcctNum(String acctNum) {
        this.acctNum = acctNum;
    }
    public void setBalance(double balance) {
        this.balance = balance;
    }
    public void withdraw(double amt) {
        balance -= amt;
        transactions.add(new Transaction('W',amt,balance,"withdrawal"));
    }
    public void deposit(double amt) {
        DepositWindow d = new DepositWindow();
        if (d != null){
           amt = d.getAmount();
           balance += amt;
           transactions.add(new Transaction('D',amt,balance,"deposit"));
        }
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the transactions
     */
    public ArrayList<Transaction> getTransactions() {
        return transactions;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }



}

最佳答案

如果将对话框设置为模态对话框,则可以在DepositWindow#getAmount调用返回后直接调用setVisible(true)(因为它将阻塞,直到用户关闭窗口为止)...

public class DepositWindow extends JDialog
{
    //...
    public DepositWindow() {
        setModal(true);
        //...


然后...

public void actionPerformed(ActionEvent e) {
    DepositWindow frame = new DepositWindow();
    frame.setTitle("Deposit Window");
    //frame.setSize(400, 250);
    frame.setModal(true);
    frame.pack();
    frame.setLocationRelativeTo(null); // Center the frame
    frame.setVisible(true);
    double amount = frame.getAmount();
    // Update the account details
}


有关更多详细信息,请参见How to Make Dialogs

07-24 09:19