如何使用 JProgressBar 作为密码强度计,它应该在我键入时改变颜色和值

发布于 2024-11-30 16:31:38 字数 380 浏览 1 评论 0原文

我正在开发逻辑来为我的登录表单构建一个良好的密码强度检查器,但问题是如何表示输入的密码强度?我正在使用 java,并且使用这种方法:

  1. 使用 JProgressBar 作为强度计,当 JPasswordField 失去焦点或在 JPasswordField 中释放按键时,它会改变颜色(这会提供更快的响应)。

  2. 我可以使用 Swing Worker 来让它变得更好吗?我从未使用过它,所以如果这是最好的方法,任何人都可以帮助我。

请原谅我的长句子。

请参见下图:


下面的图片显示了我想要实现的目标

I am developing the logic to build a good password strength checker for my login form,but the problem is how to express strength of password entered ? I am using java and I am using this approach:

  1. using JProgressBar as strength meter,it changes color when focus from JPasswordField is lost or when a key is released in JPasswordField (this gives quicker response).

  2. Can I use Swing Worker on this to make it better ? I have never used it so can anybody help me with that if it is best way.

Please forgive me for long sentences.

See Image below:


This image below shows what I want to achieve

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

单调的奢华 2024-12-07 16:31:38

在这种情况下, SwingWorker 没有让我有意义,这就是 DocumentListener

注意:此示例不是关于如何检查密码强度,而是关于如何侦听来自 JPasswordField 并将输出重定向到 JProgressBar

在此处输入图像描述在此处输入图像描述在此处输入图像描述

import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class TextLabelMirror {

    private JPanel mainPanel = new JPanel();
    private JPasswordField field = new JPasswordField(20);
    private JLabel label = new JLabel();
    private JLabel labelLength = new JLabel();
    private JProgressBar progressBar = new JProgressBar(0, 20);

    public TextLabelMirror() {
        field.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void changedUpdate(DocumentEvent e) {
                updateLabel(e);
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                updateLabel(e);
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                updateLabel(e);
            }

            private void updateLabel(DocumentEvent e) {
                String text = field.getText();//just example getText() is Depreciated !!!
                label.setText(text);
                labelLength.setText(" Psw Lenght -> " + text.length());
                if (text.length() < 1) {
                    progressBar.setValue(0);
                } else {
                    progressBar.setValue(text.length());
                }
            }
        });
        mainPanel.setLayout(new GridLayout(4, 0, 10, 0));
        mainPanel.add(field);
        mainPanel.add(label);
        mainPanel.add(labelLength);
        mainPanel.add(progressBar);
    }

    public JComponent getComponent() {
        return mainPanel;
    }

    private static void createAndShowUI() {
        JFrame frame = new JFrame("Password Strength Checker");
        frame.getContentPane().add(new TextLabelMirror().getComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                createAndShowUI();
            }
        });
    }
}

no SwingWorker doesn't make me sence in this case, that's just and about DocumentListener

NOTICE: this example not about How to check password strenght, just how to listening for changes from JPasswordField and redirect output to the JProgressBar

enter image description hereenter image description hereenter image description here

import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class TextLabelMirror {

    private JPanel mainPanel = new JPanel();
    private JPasswordField field = new JPasswordField(20);
    private JLabel label = new JLabel();
    private JLabel labelLength = new JLabel();
    private JProgressBar progressBar = new JProgressBar(0, 20);

    public TextLabelMirror() {
        field.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void changedUpdate(DocumentEvent e) {
                updateLabel(e);
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                updateLabel(e);
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                updateLabel(e);
            }

            private void updateLabel(DocumentEvent e) {
                String text = field.getText();//just example getText() is Depreciated !!!
                label.setText(text);
                labelLength.setText(" Psw Lenght -> " + text.length());
                if (text.length() < 1) {
                    progressBar.setValue(0);
                } else {
                    progressBar.setValue(text.length());
                }
            }
        });
        mainPanel.setLayout(new GridLayout(4, 0, 10, 0));
        mainPanel.add(field);
        mainPanel.add(label);
        mainPanel.add(labelLength);
        mainPanel.add(progressBar);
    }

    public JComponent getComponent() {
        return mainPanel;
    }

    private static void createAndShowUI() {
        JFrame frame = new JFrame("Password Strength Checker");
        frame.getContentPane().add(new TextLabelMirror().getComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                createAndShowUI();
            }
        });
    }
}
那些过往 2024-12-07 16:31:38

与 mKorbel 相同的想法,但有颜色:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PasswordChecker extends JPanel {
   private static final Color[] PB_COLORS = {Color.red, Color.yellow, Color.green};
   private static final int MAX_LENGTH = 15;
   private JPasswordField pwField1 = new JPasswordField(10);
   private JPasswordField pwField2 = new JPasswordField(10);
   private JProgressBar progBar = new JProgressBar();
   private int ins = 10;

   public PasswordChecker() {
      pwField1.addFocusListener(new FocusAdapter() {
         public void focusLost(FocusEvent e) {
            pwField1FocusLost(e);
         }
      });

      setLayout(new GridBagLayout());

      GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1.0, 10,
               GridBagConstraints.WEST, GridBagConstraints.BOTH, 
               new Insets(ins, ins, ins, ins), 0, 0);
      add(new JLabel("Password"), gbc);

      gbc = new GridBagConstraints(1, 0, 1, 1, 1.0, 10,
               GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, 
               new Insets(ins, ins, ins, ins), 0, 0);
      add(pwField1, gbc);

      gbc = new GridBagConstraints(0, 1, 1, 1, 1.0, 10,
               GridBagConstraints.WEST, GridBagConstraints.BOTH, 
               new Insets(ins, ins, ins, ins), 0, 0);
      add(new JLabel("Confirm Password"), gbc);

      gbc = new GridBagConstraints(1, 1, 1, 1, 1.0, 10,
               GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, 
               new Insets(ins, ins, ins, ins), 0, 0);
      add(pwField2, gbc);

      gbc = new GridBagConstraints(0, 2, 1, 1, 1.0, 10,
               GridBagConstraints.WEST, GridBagConstraints.BOTH, 
               new Insets(ins, ins, ins, ins), 0, 0);
      add(new JLabel("Strength"), gbc);

      gbc = new GridBagConstraints(1, 2, 1, 1, 1.0, 10,
               GridBagConstraints.EAST, GridBagConstraints.BOTH, 
               new Insets(ins, ins, ins, ins), 0, 0);
      add(progBar, gbc);
   }

   private void pwField1FocusLost(FocusEvent e) {
      // simple check, just checks length
      char[] pw = pwField1.getPassword();
      int value = (pw.length * 100) / MAX_LENGTH;
      value = (value > 100) ? 100 : value;
      progBar.setValue(value);

      int colorIndex = (PB_COLORS.length * value) / 100;
      progBar.setForeground(PB_COLORS[colorIndex]);
   }

   private static void createAndShowUI() {
      JFrame frame = new JFrame("Password Checker");
      frame.getContentPane().add(new PasswordChecker());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

Same idea as mKorbel, but with color:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PasswordChecker extends JPanel {
   private static final Color[] PB_COLORS = {Color.red, Color.yellow, Color.green};
   private static final int MAX_LENGTH = 15;
   private JPasswordField pwField1 = new JPasswordField(10);
   private JPasswordField pwField2 = new JPasswordField(10);
   private JProgressBar progBar = new JProgressBar();
   private int ins = 10;

   public PasswordChecker() {
      pwField1.addFocusListener(new FocusAdapter() {
         public void focusLost(FocusEvent e) {
            pwField1FocusLost(e);
         }
      });

      setLayout(new GridBagLayout());

      GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1.0, 10,
               GridBagConstraints.WEST, GridBagConstraints.BOTH, 
               new Insets(ins, ins, ins, ins), 0, 0);
      add(new JLabel("Password"), gbc);

      gbc = new GridBagConstraints(1, 0, 1, 1, 1.0, 10,
               GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, 
               new Insets(ins, ins, ins, ins), 0, 0);
      add(pwField1, gbc);

      gbc = new GridBagConstraints(0, 1, 1, 1, 1.0, 10,
               GridBagConstraints.WEST, GridBagConstraints.BOTH, 
               new Insets(ins, ins, ins, ins), 0, 0);
      add(new JLabel("Confirm Password"), gbc);

      gbc = new GridBagConstraints(1, 1, 1, 1, 1.0, 10,
               GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, 
               new Insets(ins, ins, ins, ins), 0, 0);
      add(pwField2, gbc);

      gbc = new GridBagConstraints(0, 2, 1, 1, 1.0, 10,
               GridBagConstraints.WEST, GridBagConstraints.BOTH, 
               new Insets(ins, ins, ins, ins), 0, 0);
      add(new JLabel("Strength"), gbc);

      gbc = new GridBagConstraints(1, 2, 1, 1, 1.0, 10,
               GridBagConstraints.EAST, GridBagConstraints.BOTH, 
               new Insets(ins, ins, ins, ins), 0, 0);
      add(progBar, gbc);
   }

   private void pwField1FocusLost(FocusEvent e) {
      // simple check, just checks length
      char[] pw = pwField1.getPassword();
      int value = (pw.length * 100) / MAX_LENGTH;
      value = (value > 100) ? 100 : value;
      progBar.setValue(value);

      int colorIndex = (PB_COLORS.length * value) / 100;
      progBar.setForeground(PB_COLORS[colorIndex]);
   }

   private static void createAndShowUI() {
      JFrame frame = new JFrame("Password Checker");
      frame.getContentPane().add(new PasswordChecker());
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文