Java getter和setter不能正常工作

Java getter和setter不能正常工作,java,swing,actionlistener,getter,setter,Java,Swing,Actionlistener,Getter,Setter,我需要帮助,我正在做一项作业,但能手和二传手都不行。 所以我使用了一个动作监听器,当点击AddAmount时,它应该将输入的金额添加到DepositAmount中,它会这样做,但当我调用getSavingBalance时;余额仍为零。对堆栈溢出不太熟悉我不能发布我的全部代码,因为他们说我需要添加更多细节,所以我只留下了最重要的内容 JButton addBtn = new JButton("Add Amount"); addBtn.addActionListener( new

我需要帮助,我正在做一项作业,但能手和二传手都不行。 所以我使用了一个动作监听器,当点击AddAmount时,它应该将输入的金额添加到DepositAmount中,它会这样做,但当我调用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());



            }
        });





    }

在操作侦听器中,您正在调用setDepositdeposit;,它增加了双重存款的价值,而不是双重储蓄余额的价值


然后调用getSavingBalance,它不会返回预期的金额,因为变量double savingBalance没有增加-这意味着在getter中没有任何逻辑来计算那里的内容?

在操作侦听器中调用SetDepository;,它增加了双重存款的价值,而不是双重储蓄余额的价值


然后调用getSavingBalance,它不会返回预期的金额,因为变量double savingBalance没有增加-假设您在getter中没有任何逻辑来计算那里的内容?

在您的操作侦听器中,存款时不会更新savingBalance。您应该在调用setDeposit时更新savingBalance。

在您的操作侦听器中,存款时不会更新savingBalance。在调用setDeposit时,您应该更新savingBalance。

您不必发布所有代码,但必须发布足够的代码来演示问题。忽略getSavingBalance的代码是很奇怪的,因为它是问题的一个重要部分,不是吗?这个问题应该被命名为:getter和setter按照他们应该的方式工作,但我不知道是什么错你不必发布所有的代码,但你必须发布足够的代码来演示问题。忽略getSavingBalance的代码是很奇怪的,因为它是问题的一个重要部分,不是吗?这个问题应该被命名为:Getter和Setter按照他们应该的方式工作,但我不知道有什么不对很高兴我能帮上忙!很高兴我能帮忙!