为什么我的按钮不起作用?

时间:2014-04-20 21:38:05

标签: java swing user-interface

我正在尝试为ATM机编写菜单。我正在使用MVC架构,我的代码如下所示:

存款视图:

public class Deposit extends JFrame {

private JPanel contentPane;
private JTextField textField;
JButton btnCompleteDeposit;

/**
 * Launch the application.
 */
public static void main(String[] args) {

}

/**
 * Create the frame.
 */
public Deposit() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblNewLabel = new JLabel("Deposit");
    lblNewLabel.setFont(new Font("Verdana", Font.PLAIN, 14));
    lblNewLabel.setBounds(38, 48, 195, 14);
    contentPane.add(lblNewLabel);

    JLabel lblAmount = new JLabel("Amount: $");
    lblAmount.setFont(new Font("Verdana", Font.PLAIN, 14));
    lblAmount.setBounds(38, 73, 195, 14);
    contentPane.add(lblAmount);

    textField = new JTextField();
    textField.setBounds(139, 67, 252, 20);
    contentPane.add(textField);
    textField.setColumns(10);

    btnCompleteDeposit = new JButton("Complete Deposit");
    btnCompleteDeposit.setBounds(266, 98, 125, 23);
    contentPane.add(btnCompleteDeposit);

    JButton btnGoBack = new JButton("Go Back");
    btnGoBack.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            dispose();
        }
    });
    btnGoBack.setBounds(163, 98, 89, 23);
    contentPane.add(btnGoBack);
}

void addDepositListener(ActionListener listenForDepositButton){
    btnCompleteDeposit.addActionListener(listenForDepositButton);
}

public float getDepositAmount(){
    return Float.parseFloat(textField.getText());
}

}

存款模式:

public void depositCalculation (float DepositTextField){
    System.out.println("Deposit button working");
    float finalValue = current.getBalance() + DepositTextField;
    current.setBalance(finalValue);

    JOptionPane.showMessageDialog(null, "New balance: " + current.getBalance(), "Deposit Successful", JOptionPane.INFORMATION_MESSAGE);
}

存款控制:

public class ATMController {

    private ATMModel theModel;
    private Login loginView;
    private Withdraw withdrawView;
    private Deposit depositView;
    //private Transfer transferView;

    public ATMController (Login loginView, ATMModel theModel, Withdraw withdrawView, Deposit depositView){
    this.loginView = loginView;
    this.theModel = theModel;
    this.withdrawView = withdrawView;
    this.depositView = depositView;
    //this.transferView = transferView;

    this.loginView.addLoginListener(new LoginListener());
    this.withdrawView.addWithdrawListener(new WithdrawListener());
    this.depositView.addDepositListener(new DepositListener());
    //this.transferView.addTransferListener(new TransferListener());
}

有问题的代码:

  public class DepositListener implements ActionListener{
public void actionPerformed (ActionEvent Deposit){
    System.out.println("it reads");
    float depositAmount = 0;

    try{
        depositAmount = depositView.getDepositAmount();
        theModel.depositCalculation(depositAmount);
    }
    catch (NumberFormatException ex){
        JOptionPane.showMessageDialog(null,"You must enter a number value!", "Error", JOptionPane.ERROR_MESSAGE);
    }
}

}

据我所知,我的动作听众布局合理,我的功能应该触发。然而,当我按下按钮时,没有任何反应。我尝试实现一系列'System.out.println(“x is working”)'来发现'addDepositListener'正在工作,但'DepositListener'没有响应,我无法弄清楚原因。我一直在寻找没有结果的时间。任何调试技巧将不胜感激。请帮忙。

0 个答案:

没有答案