从JDialog返回一个double值到JFrame?

时间:2014-11-14 05:17:07

标签: java swing jdialog

我正在制作银行项目并遇到问题。我有一个Depositwindow类,它扩展为JDialogAccountwindow类,扩展为JFrame。我的问题是如何通过在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;
    }
    
    
    
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

如果您创建对话框模态,则可以在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