【发布时间】:2014-11-14 05:17:06
【问题描述】:
我正在做一个银行项目,但遇到了问题。我有一个延伸到JDialog 的Depositwindow 类和一个延伸到JFrame 的Accountwindow 类。我的问题是如何通过在Depositwindow 类中添加以前的余额和存款金额来更新我的Accountwindow 余额并将其显示在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;
}
}
【问题讨论】:
-
1) 没有必要扩展
JFrame或JDialog,而且可能适得其反。 2) 使对话框模态化。 3) 通过JSpinner和SpinnerNumberModel传递对话框。 4)一旦对话框关闭(即下一个代码行),查询数字模型的值。 -
BTW 1) 源代码中的一个空白行是曾经所需要的。
{之后或}之前的空行通常也是多余的。 2) 我可能只是使用带有面板的JOptionPane.showConfirmDialog(..)(而不是JDialog)来获取详细信息。返回值将确定用户是否取消了交易。 3) 当涉及到金钱时,最好处理整数值(以美分为单位)。