Java 如何使用JProgressBar作为密码强度计,它应该随着I类型的改变而改变颜色和值
我正在开发逻辑,为我的登录表单构建一个好的密码强度检查器,但问题是如何表示输入的密码强度?我正在使用java,我正在使用这种方法:Java 如何使用JProgressBar作为密码强度计,它应该随着I类型的改变而改变颜色和值,java,swing,interactive,swingworker,Java,Swing,Interactive,Swingworker,我正在开发逻辑,为我的登录表单构建一个好的密码强度检查器,但问题是如何表示输入的密码强度?我正在使用java,我正在使用这种方法: 使用JProgressBar作为强度计,当JPasswordField的焦点丢失或在JPasswordField中释放一个键时,它会改变颜色(这会提供更快的响应) 我可以用Swing Worker来改善它吗?我从来没有用过它,所以如果这是最好的方法,有人能帮我吗 请原谅我的长句 见下图: 不,在这种情况下,我不觉得有什么意义,那只是 注意:这个示例并不是关于如何检
不,在这种情况下,我不觉得有什么意义,那只是 注意:这个示例并不是关于如何检查密码强度,而是关于如何侦听来自的更改并将输出重定向到
导入java.awt.GridLayout;
导入javax.swing.*;
导入javax.swing.event.DocumentEvent;
导入javax.swing.event.DocumentListener;
公共类TextLabelMirror{
private JPanel mainPanel=new JPanel();
私有JPasswordField=新的JPasswordField(20);
专用JLabel标签=新JLabel();
私有jlabellength=新JLabel();
私有JProgressBar progressBar=新的JProgressBar(0,20);
公共文本LabelMirror(){
field.getDocument().addDocumentListener(新DocumentListener()){
@凌驾
公共作废更改日期(记录事件e){
更新标签(e);
}
@凌驾
公共作废插入更新(文档事件e){
更新标签(e);
}
@凌驾
公共作废移除更新(文档事件e){
更新标签(e);
}
私有void updateLabel(Documente事件){
String text=field.getText();//仅示例getText()被折旧!!!
label.setText(文本);
labelLength.setText(“Psw length->”+text.length());
if(text.length()<1){
progressBar.setValue(0);
}否则{
progressBar.setValue(text.length());
}
}
});
设置布局(新的网格布局(4,0,10,0));
主面板。添加(字段);
主面板。添加(标签);
主面板。添加(标签长度);
主面板。添加(进度条);
}
公共JComponent getComponent(){
返回主面板;
}
私有静态void createAndShowUI(){
JFrame frame=新JFrame(“密码强度检查器”);
frame.getContentPane().add(新文本LabelMirror().getComponent());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
公共静态void main(字符串[]args){
invokeLater(new Runnable()){
@凌驾
公开募捐{
createAndShowUI();
}
});
}
}
与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();
}
});
}
}
如果您没有长时间运行的后台任务,我认为没有必要使用SwingWorker。您的方法是否明显减慢或冻结了GUI?我同时编写了与您类似的代码,但使用了焦点侦听器和颜色更改。不过我更喜欢你的文档。1+我真的很欣赏你,GridBag+颜色++1.
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();
}
});
}
}