当用户名或密码文本框为空时,我试图显示一个消息框但是当我运行项目时,显示的唯一文本框是“JOptionPane.showMessageDialog(null,”您的用户名或密码不正确“);”请帮忙,谢谢!
private void jButton1ActionPerformed(ActionEvent evt) {
String user=txtUsername.getText();
char[] pass=txtPassword.getPassword();
if(user == null) {
JOptionPane.showMessageDialog(null,"Username Field is empty");
} else if(pass==null) {
JOptionPane.showMessageDialog(null,"Password Field is empty");
} else {
String sql="select * from account where username=? and password=?";
try{
pst=conn.prepareStatement(sql);
pst.setString(1,txtUsername.getText());
pst.setString(2,txtPassword.getText());
rs=pst.executeQuery();
if (rs.next()) {
GridlockMain a=new GridlockMain();
a.setVisible(true);
} else {
JOptionPane.showMessageDialog(null,"Your Username or Password is incorrect");
txtUsername.setText(null);
txtPassword.setText(null);
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null,e);
}
}
}
答案 0 :(得分:1)
JTextField.getText()
不会返回null
。尝试在isEmpty
条件下使用if
方法检查值。
String user=txtUsername.getText();// It return empty String ""
// even no data is entered.
if(user.isEmpty){
JOptionPane.showMessageDialog(null,"Username Field is empty");
}
......
答案 1 :(得分:0)
试试这个..
if(!user.length() > 0){
JOptionPane.showMessageDialog(null,"Username Field is empty");
}
else if(!pass.length > 0){
JOptionPane.showMessageDialog(null,"Password Field is empty");
}
答案 2 :(得分:0)
它返回一个空字符串“”比较
StringUtils.isEmpty(txtUsername.getText())
答案 3 :(得分:0)
我还在文本验证过程中遇到了困难。我找到了一种非常简单的测试方法。在这里,我只是不允许他们进入,或者按下按钮,而不是使用JOptionPane
向用户显示消息。这是我的代码:
//Validate whether user Has input some information:
if(UserNameTA.getText() == null || UserNameTA.getText().trim().isEmpty())
{
btnEnter.setEnabled(false);
}
else
{
//Make a new JFrame for login
new ProfileHome().setVisible(true);
frame.dispose();
}
btnEnter.setEnabled(true);
我希望这至少可以指导您取得成功。
答案 4 :(得分:0)
String user = txtUsername.getText();
String pw = txtPassword.getText();
if(user.isEmpty() || (pw.isEmpty()))
{
JOptionPane.showMessageDialog(null, "Your Username or Password is incorrect" );
}
else
{
//proceed to query
答案 5 :(得分:-1)
public void save(object sender, EventArgs e)
{
string s = Session["num"].ToString();
int num = Int32.Parse(s);
DataTable t = (DataTable)Session["table"];
DataRow row1 = t.NewRow();
for(int i=0;i<num;i++){
TextBox tt = (TextBox)(form.FindControl("txtser" + i.ToString()));
string str= tt.Text + " ";
}
t.Rows.Add(row1);
Session["table"] = t;
GridView1.EditIndex = -1;
GridView1.DataSource = t;
GridView1.DataBind();
}