Java 检查空文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20262935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:40:05  来源:igfitidea点击:

Checking of empty textbox

javanetbeansloginjtextfieldmessagebox

提问by

I am trying to show a messagebox when either the username or password textbox is empty but when I run the project the only textbox showing is "JOptionPane.showMessageDialog(null,"Your Username or Password is incorrect");" Please help and Thank You!

当用户名或密码文本框为空时,我试图显示一个消息框,但是当我运行该项目时,唯一显示的文本框是“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); 
    }
  }
}                                        

回答by Masudul

JTextField.getText()does not return nullif you keep it empty. Try to check value using isEmptymethod at ifcondition.

JTextField.getText()null如果您将其保留为空,则不会返回。尝试isEmptyif条件下使用方法检查值。

  String user=txtUsername.getText();// It return empty String "" 
                                    // even no data is entered.

  if(user.isEmpty){
     JOptionPane.showMessageDialog(null,"Username Field is empty"); 
  }
  ......

回答by subash

try this..

尝试这个..

if(!user.length() > 0){
  JOptionPane.showMessageDialog(null,"Username Field is empty"); 
  }
else if(!pass.length > 0){
  JOptionPane.showMessageDialog(null,"Password Field is empty");     
}

回答by Bilbo Baggins

It returns an empty string "" compare the

它返回一个空字符串 "" 比较

StringUtils.isEmpty(txtUsername.getText())

回答by Ali Asghar Ataie

    if (user.length() == 0) {
        JOptionPane.showMessageDialog(null, "Username Field is empty");
    } else if (pass.length() == 0) {
        JOptionPane.showMessageDialog(null, "Password Field is empty");
    }

回答by WhiteFringe

I also struggled with the text Validation process. I found a very easy way to test this. Here, instead of using JOptionPaneto show a message to the user, I just didn't allow them to enter, or press the button. Here's my code:

我也在文本验证过程中挣扎。我找到了一个非常简单的方法来测试这个。在这里,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);

I hope this at least guides you to your success.

我希望这至少能引导你走向成功。

回答by Gorgatz M

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