在actionPerformed中使用if(boolean == true)

时间:2016-04-11 16:47:45

标签: java user-interface actionlistener

我目前有一个ATM项目的课程。在我的actionPerformed中,我有4个按钮(二十,五十,一百等)。让我们坚持考虑存款。我想将chequing存款与节省不同,但将它们放在同一个java文件中。

在我的属性中我有

 private triggerChecker checker;

决定用户是否要进行chequing(true boolean或saving(false boolean)。

下面的代码将显示我的actionPerformed方法

 public void actionPerformed(ActionEvent e)
 {
  //Loop through the keypad array to see if any of the buttons were pressed 
   for (int i = 0; i < 10; i++)
   {
      if (e.getSource() == keypad[i])
      {
         //Edit the input display
         cardID = cardID + i;
         display.setText("\n\n\tPlease enter amount of money:" + "\n\t" +cardID);
      }
   }

   if(e.getSource() == keypad[11]){
        cardID = "";
        display.setText("\n\n\tPlease enter amount of money:" + "\n\t" +cardID);
  }

  if(checker.getCheck() == true){
  if (e.getSource() == twenty)
  {
      display.setText(feature.DepositTOC(20.0));
      display.setFont(new Font("Arial", Font.BOLD,18));
      display.setVisible(true);

  }

  if (e.getSource() == fifty)
  {
      display.setText(feature.DepositTOC(50.0));
      display.setFont(new Font("Arial", Font.BOLD,18));
      display.setVisible(true);

  }

  if (e.getSource() == hundred)
  {
      display.setText(account.toString());
      display.setFont(new Font("Arial", Font.BOLD,20));
      display.setVisible(true);
  }

  if (e.getSource() == other)
  {
      cardID = "";
      display.setText("\n\n\tPlease enter amount of money:" + "\n\t" +cardID);
  }
  if(e.getSource() == keypad[12]){
      double num = Double.parseDouble(cardID);
      display.setText(feature.DepositTOC(num));
      display.setFont(new Font("Arial", Font.BOLD,18));
      display.setVisible(true);

  }
  }
  if(checker.getCheck() == false){
      if (e.getSource() == twenty)
      {
          display.setText(feature.DepositTOS(20.0));
          display.setFont(new Font("Arial", Font.BOLD,18));
          display.setVisible(true);

      }

      if (e.getSource() == fifty)
      {
          display.setText(feature.DepositTOS(50.0));
          display.setFont(new Font("Arial", Font.BOLD,18));
          display.setVisible(true);

      }

      if (e.getSource() == hundred)
      {
          display.setText(account.toString());
          display.setFont(new Font("Arial", Font.BOLD,20));
          display.setVisible(true);
      }

      if (e.getSource() == other)
      {
          cardID = "";
          display.setText("\n\n\tPlease enter amount of money:" + "\n\t" +cardID);
      }
      if(e.getSource() == keypad[12]){
          double num = Double.parseDouble(cardID);
          display.setText(feature.DepositTOS(num));
          display.setFont(new Font("Arial", Font.BOLD,18));
          display.setVisible(true);

      }}
     }

我有一种感觉,我无法在actionPerformed中if(boolean == true)。如果您有想法,请告诉我,因为我愿意提供技巧和帮助,而不是计划有人为我编写一个完整的功能。感谢

检查员班级

public class triggerChecker {
private boolean checkW, checkD, checkWC, checkWS, checkDC, checkDS;

public triggerChecker(){
    checkW = false;
    checkD = false;
    checkWC = false;
    checkWS = false;
    checkDC = false;
    checkDS = false;
}

public void setCheckW(boolean s){
    checkW = s;
}
public void setCheckD(boolean s){
    checkD = s;
}
public void setCheckWC(boolean s){
    checkWC = s;
}
public void setCheckWS(boolean s){
    checkWS = s;
}
public void setCheckDC(boolean s){
    checkDC = s;
}
public void setCheckDS(boolean s){
    checkDS = s;
}


public boolean getCheckW(){
    return checkW;
}
public boolean getCheckD(){
    return checkD;
}
public boolean getCheckWC(){
    return checkWC;
}
public boolean getCheckWS(){
    return checkWS;
}
public boolean getCheckDC(){
    return checkDC;
}
public boolean getCheckDS(){
    return checkDS;
}

}

1 个答案:

答案 0 :(得分:0)

没有理由不能在actionPerformed中访问或使用布尔值。您唯一需要注意的是actionPerformed可以由您的用户在您不期望的时候调用,因此请确保您的boolean始终初始化,用户可以执行您正在侦听的操作。