使用JPasswordField更改密码 [英] Change password using JPasswordField

查看:83
本文介绍了使用JPasswordField更改密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

程序将初始化密码.然后,如果用户要更改密码,则将单击更改密码"按钮.然后,程序将打开一个新窗口,然后用户将必须输入当前密码,新密码并确认密码,然后如果再次登录,密码将会更改. 我的程序的问题是它不更改其密码,并且始终是初始化的密码.我该如何解决?这是我的代码:

The program will initialized a password.Then if the user wants to change the password, they'll click the change password button. Then, the program will open a new window then the user will have to input the current password, new password, and confirm password then if you login again, the password will change. My program's problem is that it does not change its password and is always the initialized password. How do i fix this? Here's my code:

//FinalLoginForm class
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.ActionListener;
public class FinalLoginForm extends JFrame implements ActionListener{
    JLabel unLabel;
    JLabel pwLabel;
    JTextField unTextField;
    JPasswordField pwPasswordField;
    final JButton okButton = new JButton("OK");
    final JButton newPwButton = new JButton("Change Password");
    static FinalLoginForm myLogin = new FinalLoginForm();
    static FinalChangePassword change = new FinalChangePassword();
    String password = "Mariel97!";

    public FinalLoginForm(){
        super("Login Window");
        setLayout(null);
        unLabel = new JLabel("Username: ");
        pwLabel = new JLabel("Password: ");
        unTextField = new JTextField("");
        pwPasswordField = new JPasswordField("");
        okButton.setName("OK");
        newPwButton.setName("Change Password");

        unLabel.setBounds(40,60,100,30);     unTextField.setBounds(120,60,100,30);
        pwLabel.setBounds(40,90,100,30);     pwPasswordField.setBounds(120,90,100,30);
        okButton.setBounds(20,120,100,30);   newPwButton.setBounds(120,120,150,30);

        add(unLabel);
        add(pwLabel);
        add(unTextField);
        add(pwPasswordField);
        add(okButton);       
        add(newPwButton);  

        okButton.addActionListener(this);
        newPwButton.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e){
        if(okButton.getName().equals(((Component)e.getSource()).getName())){
            if(unTextField.getText().equals("mvbasbas")&& (new String(pwPasswordField.getPassword())).equals(password)){
                JOptionPane.showMessageDialog(this, "Welcome "+unTextField.getText()+"!", "Welcome!", JOptionPane.INFORMATION_MESSAGE);
            }
            else 
                JOptionPane.showMessageDialog(this, "Invalid username or password!", "Error!", JOptionPane.ERROR_MESSAGE);
        }
        else if(newPwButton.getName().equals(((Component)e.getSource()).getName())){
                FinalChangePassword changes = new FinalChangePassword();
                changes.setVisible(true);
                changes.setSize(300, 300);
                changes.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                changes.setResizable(false);
                myLogin.setVisible(false);JOptionPane.showMessageDialog(this, "Welcome "+msg+"!", "Login Form", JOptionPane.WARNING_MESSAGE);
            }
    }

    public static void main(String args[]){        
        myLogin.setVisible(true);
        myLogin.setResizable(false);
        myLogin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myLogin.setSize(300,300);
    }
}

空白空间

//FinalChangePassword
import java.util.regex.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.ActionListener;
public class FinalChangePassword extends JFrame implements ActionListener{
    JLabel currentPwLabel;
    JLabel newPwLabel;
    JLabel confirmPwLabel;
    JPasswordField currentPasswordField;
    JPasswordField newPasswordField;
    JPasswordField confirmPasswordField;
    JButton okButton;
    JButton cancelButton;
    static FinalLoginForm myLogin = new FinalLoginForm();
    static FinalChangePassword change = new FinalChangePassword();
    String currentPassword;
    String newPassword;
    String confirmPassword;

    public FinalChangePassword(){
        super("Change Password");
        setLayout(null);
        currentPwLabel = new JLabel("Current Password: ");
        newPwLabel = new JLabel("New Password: ");
        confirmPwLabel = new JLabel("Confirm Password: ");
        currentPasswordField = new JPasswordField("");
        newPasswordField = new JPasswordField("");
        confirmPasswordField = new JPasswordField("");
        okButton = new JButton("OK");
        cancelButton = new JButton("Cancel");

        currentPwLabel.setBounds(30,60,200,35);    currentPasswordField.setBounds(145,60,100,30);
        newPwLabel.setBounds(30,90,100,35);        newPasswordField.setBounds(145,90,100,30);
        confirmPwLabel.setBounds(30,120,200,35);   confirmPasswordField.setBounds(145,120,100,30);
        okButton.setBounds(45,150,100,30);         cancelButton.setBounds(145,150,100,30);

        add(currentPwLabel);
        add(newPwLabel);
        add(confirmPwLabel);
        add(currentPasswordField);
        add(newPasswordField);
        add(confirmPasswordField);
        add(okButton);       
        add(cancelButton);  

        okButton.addActionListener(this);
        cancelButton.addActionListener(this);
    }

     public static void main(String args[])throws NullPointerException{
        change.setVisible(true);
        change.setSize(300, 300);
        change.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        change.setResizable(false);
    }

     public void actionPerformed(ActionEvent e){
        if(e.getSource()==okButton){
                int response = JOptionPane.showConfirmDialog(this, "Do you want to change your password?", "Changing",JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
                if(response == JOptionPane.YES_OPTION){
                    char[] curPw = currentPasswordField.getPassword();
                    currentPassword = new String(curPw);
                    char[] newPw = newPasswordField.getPassword();
                    newPassword = new String(newPw);
                    char[] conPw = confirmPasswordField.getPassword();
                    confirmPassword = new String(conPw);
                    if(myLogin.password.equals(currentPassword)){
                        if(newPassword.length()>7){
                            if(newPassword.equals(myLogin.password))
                                JOptionPane.showMessageDialog(this, "Password already exists!", "Error!", JOptionPane.ERROR_MESSAGE);
                            else{
                                String regex = "^(?=.*?\\p{Lu})(?=.*?[\\p{L}&&[^\\p{Lu}]])(?=.*?\\d)" + "(?=.*?[`~!@#$%^&*()\\-_=+\\\\\\|\\[{\\]};:'\",<.>/?]).*$";
                                Pattern p = Pattern.compile(regex);
                                Matcher m = p.matcher(newPassword);
                                if(m.matches()){
                                    if(newPassword.equals(confirmPassword)){
                                        myLogin.password = newPassword;
                                        FinalLoginForm myLogins = new FinalLoginForm();
                                        myLogins.setVisible(true);
                                        myLogins.setResizable(false);
                                        myLogins.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                                        myLogins.setSize(300,300);
                                        change.setVisible(false);
                                    }
                                    else{
                                        JOptionPane.showMessageDialog(this, "Password does not match!", "Error!", JOptionPane.ERROR_MESSAGE);
                                    }
                                }
                                else{
                                    JOptionPane.showMessageDialog(this, "Must have Capital Letter, Small Letter, Number and Special Character!", "Error!", JOptionPane.ERROR_MESSAGE);
                                }
                            }

                        }
                        else
                            JOptionPane.showMessageDialog(this, "Must have at least 8 characters!", "Error!", JOptionPane.ERROR_MESSAGE);
                    }
                    else
                            JOptionPane.showMessageDialog(this, "Wrong current password!", "Error!", JOptionPane.ERROR_MESSAGE);
                }
        }
        else
        {
            FinalLoginForm myLogins = new FinalLoginForm();
            myLogins.setVisible(true);
            myLogins.setResizable(false);
            myLogins.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            myLogins.setSize(300,300);
            change.setVisible(false);
        }
    }
}

推荐答案

主要问题是您要在FinalChangePassword中创建FinalLoginForm的新实例,该实例与原始登录表单或新密码.

The primary problem is you're creating a new instance of FinalLoginForm in your FinalChangePassword which has no context or relationship to the original login form or the new password.

基本答案是使用模式对话框向用户请求信息,然后根据用户的选择做出决定.

The basic answer is to use a modal dialog to request information from the user and then make decisions based on the choices that the user makes.

例如,关注代码的每个区域的职责是什么,更改密码"视图实际上并不负责用当前密码值来验证旧密码值,具体取决于哪个视图实际上具有当前密码(或其他验证器). 更改密码"视图应侧重于从用户那里获取所需的信息.

Focus on what the responsibility of each area of your code is, for example, the "change password" view is probably not actually responsible for validating the old password value with the current password value, that should be up to the view which actually has the current password (or some other validator). The "change password" view should focus on getting the required information from the user.

例如...

public class ChangePasswordPane extends JPanel {
    private JPasswordField oldPassword;
    private JPasswordField newPassword;

    public ChangePasswordPane() {
        setLayout(new GridBagLayout());
        oldPassword = new JPasswordField(20);
        newPassword = new JPasswordField(20);

        GridBagConstraints gbc = new GridBagConstraints();

        gbc.insets = new Insets(2, 2, 2, 2);
        gbc.gridx = 0;
        gbc.gridy = 0;
        add(new JLabel("Old password: "), gbc);
        gbc.gridy++;
        add(new JLabel("New password: "), gbc);

        gbc.gridx++;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridy = 0;
        add(oldPassword, gbc);
        gbc.gridy++;
        add(newPassword, gbc);
    }

    public char[] getOldPassword() {
        return oldPassword.getPassword();
    }

    public char[] getNewPassword() {
        return newPassword.getPassword();
    }
}

相当基本.只是一个JPanel,其中有两个JPasswordField,一个用于旧密码,一个用于新密码.

Pretty basic. All it is is a JPanel with two JPasswordFields, one for the old and one for the new password.

现在,我们需要某种方式来实际显示面板并从中获取值...

Now, we need some way to actually show the panel and get the values from it...

public class TestPane extends JPanel {

    // This is cheat for demonstration purposes, please don't do this
    // in production
    private char[] password = "banana".toCharArray();

    public TestPane() {
        setLayout(new GridBagLayout());
        JButton btn = new JButton("Change password");
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ChangePasswordPane pane = new ChangePasswordPane();
                switch (JOptionPane.showConfirmDialog(TestPane.this, pane, "Change Password", JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION)) {
                    case JOptionPane.OK_OPTION:
                        char[] oldPassword = pane.getOldPassword();
                        if (Arrays.equals(password, oldPassword)) {
                            password = pane.getNewPassword();
                            JOptionPane.showMessageDialog(TestPane.this, "Password was changed");
                        } else {
                            JOptionPane.showMessageDialog(TestPane.this, "Password mismatch, your old password does not match your current password");
                        }
                        break;
                }
            }
        });

        add(btn);
    }

}

现在,这仅使用JOptionPane来显示ChangePasswordPane,但是如果需要,您也可以创建自己的JDialog.这会提示用户输入旧密码和新密码,并在用户按[确定]时对其进行验证

Now, this simply uses a JOptionPane to show the ChangePasswordPane, but you could also create your own JDialog if you wanted. This prompts the user for the old and new passwords and when the user presses [Okay], validates them

这篇关于使用JPasswordField更改密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆