getter 和 setter 无法正常工作
Getters and setters not working as it should
我需要帮助,我正在处理一项作业,但 getter 和 setter 无法正常工作。
所以我在单击添加金额时使用动作侦听器它应该将输入的金额添加到存款金额并且它会这样做但是当我调用getSavingBalance()时;余额仍然为零。不太熟悉堆栈溢出无法 post 我的整个代码,因为他们说我需要添加更多细节,所以我只留下最重要的部分。
JButton addBtn = new JButton("Add Amount");
addBtn.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent event) {
double deposit= Double.parseDouble(balance.getText());
setDeposit(deposit);
accBal.setText("Account Balance: "+getSavingBalance());
}
public class Bank {
int accountNumber;
static String accountType;
static double savingBalance;
static double deposit;
public Bank() {
this.accountNumber = 85061;
accountType = "Savings";
}
public static void setDeposit(double depos) {
deposit = deposit+depos;
}
public static void setSavingBalance(double saving) {
savingBalance = saving;
savingBalance+= deposit -withdraw;
}
public static void savingsFrame() {
JLabel accBal = new JLabel("Account Balance: "+ getSavingBalance());
JFrame savingsFrame = new JFrame("Bank Account");
savingsFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel savingsPanel= new JPanel(new GridLayout(2,2,1,1));
//savingsPanel.setBackground(Color.gray);
savingsPanel.setPreferredSize(new Dimension(550,100));
TitledBorder savingsTitle = new TitledBorder("Savings Account");
savingsTitle.setTitleJustification(TitledBorder.CENTER);
savingsPanel.setBorder(savingsTitle);
JLabel bal = new JLabel("Deposit Amount: ");
JTextField balance = new JTextField();
JLabel draw = new JLabel("Withdraw Amount: ");
JTextField withdraw = new JTextField();
JButton addBtn = new JButton("Add Amount");
addBtn.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent event) {
double deposit= Double.parseDouble(balance.getText());
setDeposit(deposit);
accBal.setText("Account Balance: "+getSavingBalance());
}
});
JButton withdrawBtn = new JButton("Withdraw Amount");
withdrawBtn.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent event) {
}
});
JButton updateBtn = new JButton("Update Account");
updateBtn.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent event) {
accBal.setText("Account Balanceee: "+ getSavingBalance());
}
});
}
在您调用 setDeposit(deposit);
的动作侦听器中,它会增加 double deposit
而不是 double savingBalance
的值。
然后您正在调用 getSavingBalance()
,这不会 return 预期的数量,因为变量 double savingBalance
没有递增 - 假设您在 getter 在那里计算一些东西?
在您的动作侦听器中,savingBalance
在您存款时不会更新。您应该在调用 setDeposit
时更新 savingBalance
。
我需要帮助,我正在处理一项作业,但 getter 和 setter 无法正常工作。 所以我在单击添加金额时使用动作侦听器它应该将输入的金额添加到存款金额并且它会这样做但是当我调用getSavingBalance()时;余额仍然为零。不太熟悉堆栈溢出无法 post 我的整个代码,因为他们说我需要添加更多细节,所以我只留下最重要的部分。
JButton addBtn = new JButton("Add Amount");
addBtn.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent event) {
double deposit= Double.parseDouble(balance.getText());
setDeposit(deposit);
accBal.setText("Account Balance: "+getSavingBalance());
}
public class Bank {
int accountNumber;
static String accountType;
static double savingBalance;
static double deposit;
public Bank() {
this.accountNumber = 85061;
accountType = "Savings";
}
public static void setDeposit(double depos) {
deposit = deposit+depos;
}
public static void setSavingBalance(double saving) {
savingBalance = saving;
savingBalance+= deposit -withdraw;
}
public static void savingsFrame() {
JLabel accBal = new JLabel("Account Balance: "+ getSavingBalance());
JFrame savingsFrame = new JFrame("Bank Account");
savingsFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel savingsPanel= new JPanel(new GridLayout(2,2,1,1));
//savingsPanel.setBackground(Color.gray);
savingsPanel.setPreferredSize(new Dimension(550,100));
TitledBorder savingsTitle = new TitledBorder("Savings Account");
savingsTitle.setTitleJustification(TitledBorder.CENTER);
savingsPanel.setBorder(savingsTitle);
JLabel bal = new JLabel("Deposit Amount: ");
JTextField balance = new JTextField();
JLabel draw = new JLabel("Withdraw Amount: ");
JTextField withdraw = new JTextField();
JButton addBtn = new JButton("Add Amount");
addBtn.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent event) {
double deposit= Double.parseDouble(balance.getText());
setDeposit(deposit);
accBal.setText("Account Balance: "+getSavingBalance());
}
});
JButton withdrawBtn = new JButton("Withdraw Amount");
withdrawBtn.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent event) {
}
});
JButton updateBtn = new JButton("Update Account");
updateBtn.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent event) {
accBal.setText("Account Balanceee: "+ getSavingBalance());
}
});
}
在您调用 setDeposit(deposit);
的动作侦听器中,它会增加 double deposit
而不是 double savingBalance
的值。
然后您正在调用 getSavingBalance()
,这不会 return 预期的数量,因为变量 double savingBalance
没有递增 - 假设您在 getter 在那里计算一些东西?
在您的动作侦听器中,savingBalance
在您存款时不会更新。您应该在调用 setDeposit
时更新 savingBalance
。