【发布时间】:2014-03-18 00:41:40
【问题描述】:
我的 updateButtonResults 按钮有一个小问题。我有 JOptionPane 消息对话框,当用户更新 First Name、Last Name、E-mail 和注册日期 四个字段时,它们会弹出。我的问题是弹出所有 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)
{
}
}
});
我正在尝试学习浏览我的代码,我已经调试过它,但仍然无法找出逻辑错误。
感谢您的帮助
【问题讨论】:
-
好吧,这个监听器的主体有一个
if(fname...,它将在if部分和else部分显示一个对话框,所以无论如何它都会显示一个对话框。然后它有if(lname...,它还在if部分和else部分中显示一个对话框,因此无论如何也显示一个对话框。然后你还有两个相同的ifs。无论代码采用哪条路径,它们都会显示对话框。如果您希望它在某些情况下不显示对话框,则必须添加逻辑来告诉它在这些情况下不显示对话框。 -
如果不知道 (1) 你的
fNameTextBoxResults、lNameTextBoxResults是什么类型的组件,以及 (2) 在什么条件下你想要对话框,我想我无法提供更多帮助要显示的框(文本与以前的版本不同?用户在文本字段中输入了一个键?还有什么?)。 -
是的,如果用户正确更新了名称,我会显示对话框,在这种情况下,应该会显示一个对话框,说明特定字段已更新。如果用户错误地输入更新的字段,我也会弹出一个对话框,在这种情况下,我会弹出对话框并说请以正确的格式输入字段。我的问题在于“if”语句中的所有对话框。
-
它们都是 JTextFields。条件是仅在特定文本字段更新时弹出。
标签: java logic try-catch actionlistener