繁体   English   中英

如何阻止Actionlistener重复先前的操作

[英]How to stop actionlistener from repeating previous actions

我对Java和编码一般来说是新手。 我正在尝试更新信息,但是每次更新不止一次时,它会重复以前的更新,例如,如果更新了帐户3,之后又更新了帐户4,则它通常会更新4,而空白会更新3。 如何使它仅更新我想要的内容?

updateBtn.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
                  menuScreen.setVisible(false);
                  updateScreen.setVisible(true);
                  String id = JOptionPane.showInputDialog("Membership to update (1-4)");

                  if (id.equals("1")) {
                      tf_updateDOB.setText(account1.getAccountDOB());
                      tf_updatePlan.setText(account1.getAccountPlan());
                      tf_updateCard.setText(account1.getAccountCard());
                      tf_updateName.setText(account1.getAccountName());
                      updateSubmitBtn.addActionListener(new ActionListener() {
                          public void actionPerformed(ActionEvent e) {
                              account1.setAccountDOB(tf_updateDOB.getText());
                              account1.setAccountCard(tf_updateCard.getText());
                              account1.setAccountPlan(tf_updatePlan.getText());
                              account1.setAccountName(tf_updateName.getText());
                              menuScreen.setVisible(true);
                              updateScreen.setVisible(false);
                          }
                      });

                  } else if (id.equals("2")) {
                      tf_updateDOB.setText(account2.getAccountDOB());
                      tf_updatePlan.setText(account2.getAccountPlan());
                      tf_updateCard.setText(account2.getAccountCard());
                      tf_updateName.setText(account2.getAccountName());
                      updateSubmitBtn.addActionListener(new ActionListener() {
                          public void actionPerformed(ActionEvent f) {
                              account2.setAccountDOB(tf_updateDOB.getText());
                              account2.setAccountCard(tf_updateCard.getText());
                              account2.setAccountPlan(tf_updatePlan.getText());
                              account2.setAccountName(tf_updateName.getText());
                              menuScreen.setVisible(true);
                              updateScreen.setVisible(false);
                          }
                      });
                  }

欢迎来到SO。 我怀疑您的问题是您正在使用addActionListener以便每次运行代码时都添加另一个侦听器。 单击该按钮时,您添加的所有以前的命令也将运行。

最好是增加一个侦听器,以引用最后一个选择的帐户:

private class AccountUpdater implements ActionListener {
    private Account account;

    public void setAccount(Account account) {
        this.account = account;
        tf_updateDOB.setText(account.getAccountDOB());
        tf_updatePlan.setText(account.getAccountPlan());
        tf_updateCard.setText(account.getAccountCard());
        tf_updateName.setText(account.getAccountName());
    }

    @Override
    public void actionPerformed(ActionEvent f) {
        account.setAccountDOB(tf_updateDOB.getText());
        account.setAccountCard(tf_updateCard.getText());
        account.setAccountPlan(tf_updatePlan.getText());
        account.setAccountName(tf_updateName.getText());
        menuScreen.setVisible(true);                                                                                                   
        updateScreen.setVisible(false);                        
    }
}

AccountUpdater updater = new AccountUpdater();
updateSubmitBtn.addActionListener(updater);        

然后,在选项对话框之后的代码中,只需执行以下操作:

if (id.equals("1")) {
    updater.setAccount(account1);
} else if (id.equals("2")) {
    updater.setAccount(account2);
} ...

这也将使您的代码更容易阅读,重复次数更少。

如果帐户中有一个id字段,那就更好了:

List<Account> accounts = ...;
accounts.stream()
    .filter(ac -> ac.getId().equals(id))
    .findAny().ifPresent(updater::setAccount);

然后,您就不需要ifelse if语句(这通常是不良设计的标志)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM