GUI银行账户Java



我的存款和取款按钮有问题。当我点击它们时,它们不会执行任何操作。
我的目标是让用户从他们的帐户中存款和取款。
对不起,我是这个 GUI 的新手。

银行帐户文件与存款和取款计算

public class bankAccount {  
    private double balance;
    public bankAccount() {   
        balance = 0;
    }
    public bankAccount(double initialBalance) {   
        balance = initialBalance;
    }
    public void deposit(double amount) {  
        double newBalance = balance + amount;
        balance = newBalance;
    }
    public void withdraw(double amount) {   
        double newBalance = balance - amount;
        balance = newBalance;
    }
    public double getBalance() {   
        return balance;
    }
}



我在操作执行方法的此文件中遇到问题。

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
class AccountPanel extends JPanel implements ActionListener {  
    private JLabel amountLabel, resultLabel;
    private JTextField amountTextField;   
    private JButton depositButton, withdrawButton;
    private bankAccount account;
    double result;
    public AccountPanel() {
        JPanel displayPanel = new JPanel(); 
        displayPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        amountLabel = new JLabel("Amount:"); 
        displayPanel.add(amountLabel);
        amountTextField = new JTextField(13); 
        displayPanel.add(amountTextField); 
        JPanel resultPanel = new JPanel(); 
        resultPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        resultLabel = new JLabel("Balance = "); 
        resultPanel.add(resultLabel);
        //buttons
        JPanel buttonPanel = new JPanel(); 
        buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        // deposit button 
        depositButton = new JButton("Deposit"); 
        buttonPanel.add(depositButton); 
        // withdraw
        withdrawButton = new JButton("Withdraw"); 
        buttonPanel.add(withdrawButton); 
        // add panels to main panel 
        this.setLayout(new BorderLayout()); 
        this.add(displayPanel, BorderLayout.WEST);
        this.add(resultPanel, BorderLayout.SOUTH); 
        this.add(buttonPanel, BorderLayout.EAST); 
    }

    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource(); 
        if (source == depositButton) {
            double dp = Double.parseDouble(amountTextField.getText());
            double dpamount = account.getBalance() + dp;
            account.deposit(dpamount);
            result = dpamount;
            resultLabel.setText("" + result);
            depositButton.addActionListener(this); 
        }
        else if (source == withdrawButton) { 
            double wd = Double.parseDouble(amountTextField.getText());
            account.withdraw(wd);
            resultLabel.setText("" + result);
            withdrawButton.addActionListener(this); 
        } 
   }
}



import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Window;
import javax.swing.JFrame;
import javax.swing.JPanel;
class AccountFrame extends JFrame { 
    private static final int FRAME_WIDTH = 400;
    private static final int FRAME_HEIGHT = 100;
    public AccountFrame() { 
        setTitle("Bank Account"); 
        setSize(FRAME_WIDTH, FRAME_HEIGHT); 
        centerWindow(this); 
        setResizable(false); 
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        JPanel panel = new AccountPanel(); 
        this.add(panel); 
    } 
    private void centerWindow(Window w) { 
        Toolkit tk = Toolkit.getDefaultToolkit(); 
        Dimension d = tk.getScreenSize(); 
        setLocation((d.width-w.getWidth())/2, 
        (d.height-w.getHeight())/2); 
    } 
}



import javax.swing.*; 
public class Account{ 
    public static void main(String[] args) { 
        JFrame frame = new AccountFrame(); 
        frame.setVisible(true); 
    } 
} 

尝试执行以下操作,而不是使用单个actionPerformed()方法:

首先注册事件处理程序(您错过了此步骤):

  depositButton.addActionListener(new ActionListener() {
     public void actionPerformed(ActionEvent evt) {
       handleDepositButtonEvent(evt);
     }
  });

为该事件实现自定义事件处理程序:

  private void handleDepositButtonEvent(ActionEvent evt){
     double dp = Double.parseDouble(amountTextField.getText());
     double dpamount = account.getBalance() + dp;
     account.deposit(dpamount);
     result = dpamount;
     resultLabel.setText("" + result);
  }

对要捕获的每个生成事件的按钮和组件执行此操作;单独的操作方法更易于阅读和维护。

顺便说一句,不要在动作侦听器中添加动作侦听器!这毫无意义,无论如何也行不通。

if (source == depositButton) {
    double dp = Double.parseDouble(amountTextField.getText());
    ...
    depositButton.addActionListener(this); //<--- don't do this
} 

您尚未向depositButtonwithdrawButton注册任何ActionListener

在你告诉我它们是在actionPerformed方法中注册之前,你必须意识到没有任何东西在调用这个方法,你不应该真的在这里注册你的ActionListener,或者至少不是出于你的原因,它们应该添加到构造函数中,以便在程序运行时注册它

详细了解如何编写操作侦听器和如何使用按钮、复选框和单选按钮

depositButton.addActionListener(this)withdrawButton.addActionListener(this) 调用需要在 AccountPanel 构造函数中。现在它们永远不会被执行,因为这些按钮最初没有事件处理程序。

相关内容

  • 没有找到相关文章

最新更新