【问题标题】:Getting String equivalent of input in jPasswordField在 jPasswordField 中获取等效于输入的字符串
【发布时间】:2015-10-29 08:47:51
【问题描述】:

我有一个代码可以检查用户输入是否仅为字母数字,并根据标准检查输入的密码强度。如果出现以下情况,则会出现一个带有注释的标签:

-字段为

-使用了无效字符

如果密码是WEAK、MEDIUM、STRONGVERY STRONG

,也会显示
this.AlphaNumericOnly(this.jTextField1.getText(),this.warningLbl3);
this.passwordStrength(this.jTextField1.getText(),this.warningLbl4);

如果在 jtextfield 中使用,此方法可以正常工作。但是我想使用 jpasswordfield 来隐藏用户输入。

我已经试过了:

.toString();

String.valueOf();

还有这个循环:

char[] input=(this.jPasswordField1.getPassword());
 final_pass = "";
    for(char x : input) {
     final_pass += x;
    }

但该方法无法“检查”我转换的字符串。

这是我的方法..

public void AlphaNumericOnly(String input,JLabel obj){
if(!"".equals(input)){
    obj.setText(" ");
    warning=false;

    char c[] = input.toCharArray();

    int count = 0;
    for(int x=0; x<c.length; x++){
        if((!Character.isAlphabetic(c[x]))&&(!Character.isDigit(c[x]))){// && c[x]!='-' && c[x]!='(' && c[x]!=')' && c[x]!='+'&& c[x]!='/'&& c[x]!='\\'){
            count=+1;        
        }
    }
    if(count>0){
        obj.setText("*Please use valid characters.");
        warning=true;
    }else{
        obj.setText(" ");
        warning=false;
    }
}else{
    obj.setText("*This Field cannot be left Empty.");
    warning=true;
}   
}
private void passwordStrength(String input,JLabel obj){
   char c[] = input.toCharArray();
   int count=0;
   int notAlphaNumericCount=0;
   for(int j=0;j<c.length;j++){
       if(Character.isAlphabetic(c[j])){
            if(Character.isUpperCase(c[j])){
                count++;
            }
       }else if(Character.isDigit(c[j])){

       }else{
         obj.setText(" ");
         warning=false;
         notAlphaNumericCount++;
       }
   }
if(notAlphaNumericCount==0){   
    if(input.length()<1){
        obj.setText(" ");
        warning=false;
    }else if(input.length()<4){
        obj.setText("Password Strength: WEAK");
        obj.setForeground(Color.red);
    }else if(input.length()<8){
        obj.setText("Password Strength: MEDIUM");
        obj.setForeground(Color.blue);
    }else if(input.length()<10){
        obj.setText("Password Strength: STRONG");
        obj.setForeground(Color.green);
    }else if(count!=0){    
        obj.setText("Password Strength: VERY STRONG");
        obj.setForeground(Color.orange);
    }
}
}

编辑: 为了更直观的理解 以下是我如何使用此方法:

username [Textfield input]- [obj label warning]
password [Textfield input]- [obj label warning]

          [obj label which displays password strength]

【问题讨论】:

  • 查看getPassword()方法返回的char数组的内容即可。
  • @Sumit Trehan 我不太擅长转换,你能给我举个例子吗:)

标签: java string jpasswordfield


【解决方案1】:

好吧,getPassword() 函数的返回值是 char 数组。您可以通过其构造函数轻松地将其转换为字符串。

new String(this.jPasswordField1.getPassword());

不过,就我个人而言,我认为将passwordStrength 方法的参数更改为采用char 数组而不是String 会更好。因为您只使用参数 input 将其转换为 char 数组并获取其长度。 而且,每次通过这种方式构造 String 时,都会创建一个新的 String 对象。

【讨论】:

  • 抱歉,我忘了添加那行代码,我已经尝试过了,它也没有工作
  • 嗯,你能解释一下什么没用吗?有任何错误信息吗?你指的方法是AlphaNumericOnly还是passwordStrength?
猜你喜欢
  • 2022-08-17
  • 2017-05-12
  • 2021-01-05
  • 2023-01-19
  • 2021-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-05
相关资源
最近更新 更多