【问题标题】:Checking of empty textbox检查空文本框
【发布时间】:2013-12-14 07:16:16
【问题描述】:

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

【问题讨论】:

    标签: java netbeans login jtextfield messagebox


    【解决方案1】:

    JTextField.getText() 不返回 null 如果您将其留空。尝试在if 条件下使用isEmpty 方法检查值。

      String user=txtUsername.getText();// It return empty String "" 
                                        // even no data is entered.
    
      if(user.isEmpty){
         JOptionPane.showMessageDialog(null,"Username Field is empty"); 
      }
      ......
    

    【讨论】:

      【解决方案2】:

      试试这个..

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

      【讨论】:

        【解决方案3】:

        它返回一个空字符串“”比较

        StringUtils.isEmpty(txtUsername.getText())
        

        【讨论】:

          【解决方案4】:
              if (user.length() == 0) {
                  JOptionPane.showMessageDialog(null, "Username Field is empty");
              } else if (pass.length() == 0) {
                  JOptionPane.showMessageDialog(null, "Password Field is empty");
              }
          

          【讨论】:

          • 我从来没有听说过少于 0 个字符的字符串。如果你要解决这个问题,那么这个答案将与 subash 的 answer 相同,这使得这个答案毫无意义。
          【解决方案5】:

          我还在文本验证过程中苦苦挣扎。我找到了一种非常简单的方法来测试它。在这里,我没有使用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);
          

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

          【讨论】:

            【解决方案6】:
            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
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2014-07-10
              • 1970-01-01
              • 2010-12-06
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多