我的存款和取款按钮有问题。我点击他们的时候他们什么也不做。
我的目标是让用户存款并从他们的帐户中提取。
抱歉,我是刚接触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);
}
} 发布于 2014-04-07 03:41:08
尝试这样做,而不是使用单个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
} 发布于 2014-04-07 03:38:01
您没有将任何ActionListener注册到depositButton或withdrawButton。
在告诉我它们是在actionPerformed方法中注册之前,您必须意识到没有调用此方法,您不应该在这里注册您的ActionListener,或者至少由于您的原因,应该将它们添加到构造函数中,以便在程序运行时注册。
更详细地查看如何编写动作侦听器和如何使用按钮、复选框和单选按钮
发布于 2014-04-07 03:39:00
depositButton.addActionListener(this)和withdrawButton.addActionListener(this)调用需要在AccountPanel构造函数中。现在,它们将永远不会被执行,因为按钮最初没有事件处理程序。
https://stackoverflow.com/questions/22903159
复制相似问题