Java 一次验证一个字段

Java 一次验证一个字段,java,swing,validation,Java,Swing,Validation,问题是验证我需要它,在第一个文本框之后停止验证并给予注意。如果第一个文本字段为空,并且您按下了“验证”按钮,则光标应转到该字段,而不是继续验证其余字段 如何实现这一目标 而不是写作 private void loadActionPerformed(java.awt.event.ActionEvent evt) { validation(); CreateXml(); clea

问题是验证我需要它,在第一个文本框之后停止验证并给予注意。如果第一个文本字段为空,并且您按下了“验证”按钮,则光标应转到该字段,而不是继续验证其余字段

如何实现这一目标

而不是写作

private void loadActionPerformed(java.awt.event.ActionEvent evt) {                                     
        validation();
        CreateXml();
        clear();

  public void validation(){
    if (username.getText().trim().length()==0).v{

        JOptionPane.showMessageDialog(Retrycount, "Please input user name", "ERROR", JOptionPane.OK_CANCEL_OPTION);
    }


    if (PASSWORD1.getText().length()==0){

        JOptionPane.showMessageDialog(Retrycount, "Please input password","ERROR", JOptionPane.OK_CANCEL_OPTION);
    }
    if(DOMAIN1.getText().length()==0){
         JOptionPane.showMessageDialog(Retrycount, "Please input Domain name","ERROR", JOptionPane.OK_CANCEL_OPTION);

    }
     if (MYSQLPassword.getText().length() ==0){
         JOptionPane.showMessageDialog(Retrycount, "Please input MYSQLPassword","ERROR", JOptionPane.OK_CANCEL_OPTION);

     }
     if (Username_Domain.getText().length() ==0){
         JOptionPane.showMessageDialog(Retrycount, "Please input second username under FTPFileUploaderPatch","ERROR", JOptionPane.OK_CANCEL_OPTION);
     }
     if (Password2.getText().length() ==0){
         JOptionPane.showMessageDialog(Retrycount, "Please input Password under FTPFileUploaderPatch","ERROR", JOptionPane.OK_CANCEL_OPTION);
     }

     if (DestinationType1.getText().length() ==0){
         JOptionPane.showMessageDialog(Retrycount, "Please input DestinationType under FTPFileUploaderPatch","ERROR", JOptionPane.OK_CANCEL_OPTION);

     }
     if (DestinationFileName1.getText().length() ==0){
        JOptionPane.showMessageDialog(Retrycount, "Please input DestinationFileName under FTPFileUploaderPatch","ERROR", JOptionPane.OK_CANCEL_OPTION);      
    }

     if (DestinationEntry1.getText().length() ==0){
         JOptionPane.showMessageDialog(Retrycount, "Please input DestinationEntry under FTPFileUploaderPatch","ERROR", JOptionPane.OK_CANCEL_OPTION);

     }
     if (SubDirectorySearchString1.getText().length() ==0) {
         JOptionPane.showMessageDialog(Retrycount, "Please input DirectorySearchString under FTPFileUploaderPatch","ERROR", JOptionPane.OK_CANCEL_OPTION);
     }
     if (TempDirectory1.getText().length() ==0)  {
         JOptionPane.showMessageDialog(Retrycount, "Please input TempDirectory under FTPFileUploaderPatch","ERROR", JOptionPane.OK_CANCEL_OPTION);
     }

     if (SourcePath2.getText().length() ==0 ) {JOptionPane.showMessageDialog(Retrycount, "Please input SourcePath2 under FTPFileUploaderPatch","ERROR", JOptionPane.OK_CANCEL_OPTION);

     }

     if (DestinationType2.getText().length() ==0)  {
         JOptionPane.showMessageDialog(Retrycount, "Please input DestinationType2 under FTPFileUploaderPatch > ReplicationMonitor","ERROR", JOptionPane.OK_CANCEL_OPTION); 
     }
    if (DestinationType2.getText().length() ==0)  {
         JOptionPane.showMessageDialog(Retrycount, "Please input DestinationPath2 under FTPFileUploaderPatch > ReplicationMonitor","ERROR", JOptionPane.OK_CANCEL_OPTION); 
    }
    if (DestinationRootFolder2.getText().length() ==0)  {
         JOptionPane.showMessageDialog(Retrycount, "Please input DestinationRootFolder2 under FTPFileUploaderPatch > ReplicationMonitor","ERROR", JOptionPane.OK_CANCEL_OPTION); 
  } 
    if (DestinationFileName2.getText().length() ==0)  {
         JOptionPane.showMessageDialog(Retrycount, "Please input DestinationFileName2 under FTPFileUploaderPatch > ReplicationMonitor","ERROR", JOptionPane.OK_CANCEL_OPTION); 
  }
    if (DestinationEntry2.getText().length() ==0)  {
         JOptionPane.showMessageDialog(Retrycount, "Please input DestinationEntry2 under FTPFileUploaderPatch > ReplicationMonitor","ERROR", JOptionPane.OK_CANCEL_OPTION); 
    } 
     if (SubDirectorySearchString2.getText().length() ==0)  {
         JOptionPane.showMessageDialog(Retrycount, "Please input SubDirectorySearchString2 under FTPFileUploaderPatch > ReplicationMonitor","ERROR", JOptionPane.OK_CANCEL_OPTION); 
     }
     if (TempDirectory2.getText().length() ==0)  {
         JOptionPane.showMessageDialog(Retrycount, "Please input TempDirectory2 under FTPFileUploaderPatch > ReplicationMonitor","ERROR", JOptionPane.OK_CANCEL_OPTION); 
     }
     if (SourcePath3.getText().length() ==0)  {
         JOptionPane.showMessageDialog(Retrycount, "Please input SourcePath3 under FTPFileUploaderPatch > Config.properties","ERROR", JOptionPane.OK_CANCEL_OPTION); 
     }
     if (DestinationType3.getText().length() ==0)  {
         JOptionPane.showMessageDialog(Retrycount, "Please input DestinationType3 under FTPFileUploaderPatch > Config.properties","ERROR", JOptionPane.OK_CANCEL_OPTION); 
     }
    if (DestinationPath3.getText().length() ==0)  {
         JOptionPane.showMessageDialog(Retrycount, "Please input DestinationPath3 under FTPFileUploaderPatch > Config.propertiesr","ERROR", JOptionPane.OK_CANCEL_OPTION); 
    }
    if (DestinationRootFolder3.getText().length() ==0)  {
         JOptionPane.showMessageDialog(Retrycount, "Please input DestinationRootFolder3 under FTPFileUploaderPatch > Config.properties","ERROR", JOptionPane.OK_CANCEL_OPTION); 
    }
   if (DestinationFileName3.getText().length() ==0)  {
         JOptionPane.showMessageDialog(Retrycount, "Please input DestinationFileName3 under FTPFileUploaderPatch > Config.properties","ERROR", JOptionPane.OK_CANCEL_OPTION); 
   }
 if (DestinationEntry3.getText().length() ==0)  {
         JOptionPane.showMessageDialog(Retrycount, "Please input DestinationEntry3 under FTPFileUploaderPatch > Config.properties","ERROR", JOptionPane.OK_CANCEL_OPTION); 
 }
依此类推,如果构建:

 if (PASSWORD1.getText().length()==0){
     JOptionPane.showMessageDialog(Retrycount, "Please input password","ERROR", JOptionPane.OK_CANCEL_OPTION);
}
if(DOMAIN1.getText().length()==0){
     JOptionPane.showMessageDialog(Retrycount, "Please input Domain name","ERROR", JOptionPane.OK_CANCEL_OPTION);
}

每次字段验证后,将焦点设置为该字段,然后退出函数

Like(对函数名没有信心)

请学习类、方法和属性名称的通用(特别是用于名称的大小写),并一致使用它们。
PASSWORD1.getText()
将返回一个
字符串,这可能会导致安全漏洞。考虑将密码保存为<代码> char []/COD>数组,一旦你完成了,就用诸如“代码>数组”之类的东西来写他们的内容。填充(PaseDurray','')/Case>,这样就没有人能轻易地访问它。这个线程可能会引起你的兴趣:-我想你的意思是说“1”,这个方法也可以工作:
if (PASSWORD1.getText().length()==0){
    JOptionPane.showMessageDialog(Retrycount, "Please input password","ERROR", JOptionPane.OK_CANCEL_OPTION);
} else if(DOMAIN1.getText().length()==0){
    JOptionPane.showMessageDialog(Retrycount, "Please input Domain name","ERROR", JOptionPane.OK_CANCEL_OPTION);
}
if (username.getText().trim().length()==0).v{
    JOptionPane.showMessageDialog(Retrycount, "Please input user name", "ERROR", JOptionPane.OK_CANCEL_OPTION);
    username.getFocus();
    return;
}