我的updateButtonResults按钮有一个小问题。我有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
与其尝试一次验证所有输入,不如让我们尝试使此代码更加模块化。分离与特定字段相关的所有逻辑,并将其作为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
的匿名类,不如定义一个扩展它的命名类,并定义一个构造函数,通过该构造函数可以传递数据,例如文本字段的前一个值:
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));
}