我的存款和取款按钮出现问题。点击它们时它们什么都不做
我的目标是让用户存款并退出账户
对不起,我是这个GUI的新手。
bankAccount文件,存款和取款计算
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;
}
}
我在actionPerformed方法的这个文件中遇到了麻烦。
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);
}
}
答案 0 :(得分:3)
尝试执行类似这样的操作,而不是使用单个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
}
答案 1 :(得分:1)
您尚未向ActionListener
或depositButton
注册任何withdrawButton
。
在你告诉我他们是用actionPerformed
方法注册之前,你必须意识到没有任何东西在称这种方法,你不应该在这里注册你的ActionListener
,或者至少不是因为你无论如何,它们应该在构造函数中添加,以便在程序运行时注册它
详细了解How to Write an Action Listener和How to Use Buttons, Check Boxes, and Radio Buttons了解详情
答案 2 :(得分:1)
depositButton.addActionListener(this)
和withdrawButton.addActionListener(this)
调用需要位于AccountPanel
构造函数中。现在他们永远不会被执行,因为按钮最初没有事件处理程序。