获取器和设置器不能正常工作。

问题描述 投票:0回答:1

我需要帮助,我正在做一个任务,getters和setters都不工作,所以我使用一个动作监听器,当添加金额被点击时,它应该添加输入的金额到存款金额,它做到了,但当我调用getSavingBalance();余额仍然保持在零,不熟悉堆栈过流不能发布我的整个代码,因为他们说我需要添加更多的细节,所以我只是留下了什么最重要的。

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());



            }
        });





    }
java getter setter
1个回答
1
投票

在你的动作监听器中。savingBalance 当您存款时,并没有更新。您应该更新 savingBalance 当你打电话 setDeposit.


2
投票

在你的动作监听器中,你正在调用 setDeposit(deposit);的值,它增加了 double deposit 而非 double savingBalance.

然后你就叫 getSavingBalance(),它没有返回预期的金额,因为变量 double savingBalance 没有递增--假设你在getter中没有任何逻辑来计算一些东西?

© www.soinside.com 2019 - 2024. All rights reserved.