从JDialog返回double值到JFrame? [英] Return a double value from JDialog to JFrame?
问题描述
我在做一个银行项目,遇到了问题.我有一个扩展到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;
}
}
如果将对话框设置为模态,则只需在setVisible(true)
调用返回后调用DepositWindow#getAmount
(因为它将阻塞,直到用户关闭窗口为止) ...
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
}
有关详细信息,请参见如何制作对话框 >
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;
}
}
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
这篇关于从JDialog返回double值到JFrame?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!