java-为什么我的 "deposit"方法不适用于带有 gui 的 ATM?

标签 java user-interface

我正在制作一台 ATM,用户必须首先输入密码 (1234),然后一旦正确输入密码,用户就可以提取 50、100 或 200 美元,或者存款低于 1000 美元。我的提款方法都有效,但是当我运行程序并尝试存款时,输入金额并尝试点击“输入”按钮后什么也没有发生。好像我的输入按钮不起作用。这是我的存款方式使用的代码:

 if (event.getSource() == deposit) {
    instructionScreen.setText("Enter the amount you would like to deposit, then click Enter.");

    if (event.getSource() == enter) {
       saveScreen = displayInput.getText();
       double add = Double.parseDouble(saveScreen);
       if (add <= 1000) {
          balance += add;
          instructionScreen.setText("Your new balance is $" + balance + ".");
       }
       else {
          instructionScreen.setText("The maximum amount you can deposit is $1000. Please enter a new amount.");
          displayInput.setText("");
          if (event.getSource() == enter) {
             saveScreen = displayInput.getText();
             add = Double.parseDouble(saveScreen);
          }
       }

       displayInput.setText("");
       System.out.println("A deposit of $" + add + "was made. Your new balance is $" + balance);
    }
 }

当我输入存款金额后按回车键时,代码有什么问题导致我的存款没有执行任何操作?

最佳答案

如果您的程序是线性控制台程序,那么它会很好地工作,但这不是事件驱动编程的工作方式。您不需要期望源立即神奇地从存款变为输入,而是需要在选择存款时更改对象的状态,也许可以通过更改 boolean 字段,然后在输入的操作监听器中测试该字段。

例如,

if (event.getSource() == deposit) {
    instructionScreen.setText("Enter the amount you would like to deposit, then click Enter.");
    depositState = true;  // a boolean field
} else if (event.getSource() == enter) {
    if (depositState) {
        depositState = false; // reset it

        saveScreen = displayInput.getText();
        double add = Double.parseDouble(saveScreen);
        // .... etc

关于java-为什么我的 "deposit"方法不适用于带有 gui 的 ATM?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33138929/

相关文章:

Java servlet : xml validation against xsd

unit-testing - 你如何测试视觉组件?

java - 卢塞恩 : why the highlighting does an extra word splitting ?

java 128位结构位操作

java - 在 Spring Security 中使用个人资料名称或电子邮件登录

java - 同步静态方法和非静态方法的区别

c# - ASP.NET:功能键快捷方式?

python - 获取数组中按钮的索引(按下时)

java - GUI计算器按钮问题

c# - 异步绑定(bind)和 LINQ 查询挂起