gpt4 book ai didi

java - ActionListener 逻辑错误 (Java)

转载 作者:行者123 更新时间:2023-11-29 08:51:47 24 4
gpt4 key购买 nike

我的 updateButtonResults 按钮有一个小问题。我有 JOptionPane 消息对话框,当用户更新 First Name、Last Name、E-mail 和 Sign-up date 这四个字段时,这些对话框被编程为弹出。我的问题是所有 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

与其尝试一次验证所有输入,不如尝试使这段代码更模块化。分离属于特定字段的所有逻辑,并将其作为 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 来完成。否则,该按钮完全没有必要。

关于java - ActionListener 逻辑错误 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22468303/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com