问题
I'm making a bank project and came into a problem. I have a Depositwindow
class that extends to a JDialog
and an Accountwindow
class that extends to a JFrame
. My problem is how to update my Accountwindow
balance by adding the previous balance and the deposit amount in the Depositwindow
class and display it in the Accountwindow
balance?
Here is my 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();
}
}
}
Here's my 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);
}
}
Account class:
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;
}
}
回答1:
If you make the dialog modal, you can simply call DepositWindow#getAmount
after the setVisible(true)
call returns (as it will block until the user dismisses the window)...
public class DepositWindow extends JDialog
{
//...
public DepositWindow() {
setModal(true);
//...
Then...
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
}
See How to Make Dialogs for more details
来源:https://stackoverflow.com/questions/26923386/return-a-double-value-from-jdialog-to-jframe