Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ActionListener逻辑错误(Java)_Java_Logic_Try Catch_Actionlistener - Fatal编程技术网

ActionListener逻辑错误(Java)

ActionListener逻辑错误(Java),java,logic,try-catch,actionlistener,Java,Logic,Try Catch,Actionlistener,我的更新按钮结果有一个小问题。我有JOptionPane消息对话框,当用户更新四个字段时弹出名字、姓氏、电子邮件和注册日期。我的问题是所有4条消息都会弹出,即使我只更新了一个字段。示例:我更新了客户的姓氏,消息对话框将按此顺序弹出(名字、姓氏、电子邮件、注册日期) 这是我的密码 //method for buttons on 'resultFrame' public void BtnAction3() { updateButtonResult

我的更新按钮结果有一个小问题。我有JOptionPane消息对话框,当用户更新四个字段时弹出名字、姓氏、电子邮件和注册日期。我的问题是所有4条消息都会弹出,即使我只更新了一个字段。示例:我更新了客户的姓氏,消息对话框将按此顺序弹出(名字、姓氏、电子邮件、注册日期)

这是我的密码

//method for buttons on 'resultFrame'
public void BtnAction3() 
{                        
  updateButtonResults.addActionListener(
        new ActionListener()
        {
           //method for events that will be performed when updateButton is pressed
           public void actionPerformed(ActionEvent e) 
           {
              //instanciates variables to text in textfields
              String fname = fNameTextBoxResults.getText();
              String lname = lNameTextBoxResults.getText();
              String email = eMailTextBoxResults.getText();
              String signUpDate = signUpTextBoxResults.getText();

              try
              {
                 //statement that checks to make sure user enters only letters
                 if(fname.matches("[a-zA-Z]+"))
                 {
                    //updates 'Fname' field in db to text that user inputted in 'fname' textfield
                    rs2.updateString("Fname", fname);
                    JOptionPane.showMessageDialog(null, "Customer first name been updated!");
                 }

                 //statement that prompts user if they enter something other letters
                 else
                 {
                    JOptionPane.showMessageDialog(null, "Please enter first name in correct format!");
                    fNameTextBoxResults.setText("");
                 }

                 //statement that checks to make sure user enters only letters
                 if(lname.matches("[a-zA-Z]+"))
                 {   
                    //updates 'Lname' field in db to text that user inputted in 'lname' textfield                      
                    rs2.updateString("Lname", lname);
                    JOptionPane.showMessageDialog(null, "Customer last name been updated!");
                 }

                 //statement that prompts user if they enter something other letters
                 else
                 {
                    JOptionPane.showMessageDialog(null, "Please enter last name in correct format!");
                    lNameTextBoxResults.setText("");
                 }

                 //statement and actions if user enters a '.'
                 if(email.contains("."))
                 {  
                    //gets last period in "email"
                    int emailDotCheck = email.lastIndexOf(".");

                    //substring to period in variable "emailDotCheck"
                    String extensionCheck = email.substring(emailDotCheck);

                    //statement and actions if user doesn't enter email correctly           
                    if(!email.contains("@") || !extensionCheck.matches("\\.[a-z]{3}"))
                    {
                       JOptionPane.showMessageDialog(null, "Please enter email in correct format!");
                       eMailTextBoxResults.setText("");
                    }                   

                    //statement and actions if user enters email correctly
                    else
                    {
                       //updates 'E-mail' field in db to text that user inputted in 'email' textfield
                       rs2.updateString("E_mail", email);
                       JOptionPane.showMessageDialog(null, "Customer E-mail been updated!");
                    }
                 }

                 //action if user doesnt enter email correctly
                 else
                 {
                    JOptionPane.showMessageDialog(null, "Please enter email in correct format!");
                    eMailTextBoxResults.setText("");
                 }

                 //instance variables for 'signUpDate'
                 int month = 100;
                 int day = 100;
                 int year = 10000;

                 if(signUpDate.matches("\\d{2}/\\d{2}/\\d{4}"))
                 {
                    //instance variables
                    String monthStr = signUpDate.substring(0,2);
                    String dayStr = signUpDate.substring(3,5);
                    String yearStr = signUpDate.substring(6);

                    //parsing intstance variables to Integers
                    month = Integer.parseInt(monthStr);
                    day = Integer.parseInt(dayStr);
                    year = Integer.parseInt(yearStr);

                    //statement and actions if user doesn't follow correct format
                    if(month > 12 || day > 31 || year > 2100)
                    {
                       JOptionPane.showMessageDialog(null, "Please enter date in correct format! (dd/MM/yyyy)");
                       signUpTextBoxResults.setText("");
                    }

                    //statements and actions if user enters date correctly
                    else
                    {
                       //updates 'Sign-up date' field in db to text that user inputted in 'signUpDate' textfield
                       rs2.updateString("Sign_up_date", signUpDate);
                       JOptionPane.showMessageDialog(null, "Customer Sign-up date been updated!");
                    } 
                 }

                 //statement and actions if user doesn't follow correct format           
                 else 
                 {
                    JOptionPane.showMessageDialog(null, "Please enter date in correct format! (dd/MM/yyyy)");
                    signUpTextBoxResults.setText("");
                 }

                 //updates row in db
                 rs2.updateRow();
                 //JOptionPane.showMessageDialog(null, "Customer has been updated!");

              }

              catch(Exception ex)
              {

              }          
           }          
        });
我正在努力学习遍历我的代码,我已经调试过了,但仍然无法找出逻辑错误


感谢您的帮助

您有四个文本字段:

fNameTextBoxResults
lNameTextBoxResults
eMailTextBoxResults
signUpTextBoxResults
class MyActionListener extends ActionListener {  // lousy name, but ...
    public MyActionListener(String prevFirstName, String prevLastName, String prevEMail, String prevSignUp) {
        this.prevFirstName = prevFirstName; ... and so on
    }
    public void ActionPerformed(ActionEvent e) {
        ... what you already have, except that you can say things like
        if (!fname.equals(prevFirstName)) {
            ... now you can do your dialog and it won't show up unless the names
            ... are different
        }
        ...
    }
}
与其尝试一次验证所有输入,不如让我们尝试将此代码模块化一点。分离与特定字段相关的所有逻辑,并将其作为
ActionListener
添加到该字段中。例如:

 fNameTextBoxResults.addActionListener(
    new ActionListener()
    {
       public void actionPerformed(ActionEvent e) 
       {
             //statement that checks to make sure user enters only letters
             if(fname.matches("[a-zA-Z]+"))
             {
                //updates 'Fname' field in db to text that user inputted in 'fname' textfield
                rs2.updateString("Fname", fname);
                JOptionPane.showMessageDialog(null, "Customer first name been updated!");
             }

             //statement that prompts user if they enter something other letters
             else
             {
                JOptionPane.showMessageDialog(null, "Please enter first name in correct format!");
                fNameTextBoxResults.setText("");
             }
       }
    });

冲洗并重复其他三个字段。如果需要某种类型的最终确定操作,那么可以使用
updateButtonResults
来完成。否则,按钮完全不需要。

您可能需要定义一个扩展它的命名类,并定义一个构造函数,通过该构造函数可以传入数据,例如文本字段的先前值,而不是使用扩展了
ActionListener的匿名类:

fNameTextBoxResults
lNameTextBoxResults
eMailTextBoxResults
signUpTextBoxResults
class MyActionListener extends ActionListener {  // lousy name, but ...
    public MyActionListener(String prevFirstName, String prevLastName, String prevEMail, String prevSignUp) {
        this.prevFirstName = prevFirstName; ... and so on
    }
    public void ActionPerformed(ActionEvent e) {
        ... what you already have, except that you can say things like
        if (!fname.equals(prevFirstName)) {
            ... now you can do your dialog and it won't show up unless the names
            ... are different
        }
        ...
    }
}
然后设置如下:

public void BtnAction3() 
{                        
  updateButtonResults.addActionListener(
        new MyActionListener(firstName, lastName, eMail, signUp));
}

这个监听器的主体有一个
if(fname…
,它将在
if
部分和
else
部分显示一个对话框,因此不管发生什么,它都会显示一个对话框。然后它有
if(lname…
if
部分和
else
部分也会显示一个对话框,因此无论发生什么情况,也会显示一个对话框。然后,您还有两个
if
的方式相同。它们会显示对话框,无论代码走哪条路径。如果您希望它在某些情况下不显示对话框,您必须添加逻辑告诉它在这些情况下不显示对话框。如果不知道(1)您的
fNameTextBoxResults
lNameTextBoxResults
,是什么类型的组件,以及(2)您希望在什么条件下显示对话框,我想我无法提供更多的帮助(文本与上一版本不同?用户在文本字段中输入了密钥?是否有其他内容?)。是的,如果用户正确更新了名称,我会显示对话框,在这种情况下,应该会显示一个对话框,说明特定字段已更新。如果用户错误地输入更新字段,我也会弹出一个对话框,在这种情况下,我会弹出对话框,并说请以正确的格式输入字段。我的问题是h“if”语句中的所有对话框。它们都是JTextFields。条件是只有在特定的textfield更新时才会弹出。我认为所有对话框都需要在“updateButtonResults”“ActionListener”下。因为当每个if/else语句传入if语句时,它会将我的数据库更新到数据库中的特定字段。这有意义吗?是否可以执行嵌套ActionListener?这完全改变了UI的行为,因此将其称为“使代码更模块化”的一种方法不正确。重构不应该改变行为。@ajb一次验证一个输入框是否有意义?如果一个框包含不正确的数据怎么办?它仍然会推送部分更新!这样用户就可以清楚地看到,他们输入的内容会自动更新,并且在他们进行修改时会立即通知他们istake.@javaGeek看看我提供的示例。它从
fNameTextBoxResults
中获取数据,我认为其中包含客户的名字。然后,它根据您的规范验证数据。然后,它更新数据库中的
Fname
字段,或者给出错误消息。您可以为您创建的每个字段创建一个需要验证,它“将[您的]数据库更新到该特定字段…”