程序將初始化一個密碼。然後如果用戶想要更改密碼,他們將單擊更改密碼按鈕。然後,程序會打開一個新窗口,那麼用戶將不得不輸入當前密碼,新密碼和確認密碼,如果再次登錄,密碼將會改變。 我的程序的問題是它不會更改其密碼,並且始終是初始化密碼。我該如何解決?這裏是我的代碼:使用JPasswordField更改密碼
//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)" + "(?=.*?[`[email protected]#$%^&*()\\-_=+\\\\\\|\\[{\\]};:'\",<.>/?]).*$";
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);
}
}
}
不要依賴'static',使用模態對話框顯示更改密碼的請求關閉,得到的價值觀,你會比較然後 – MadProgrammer
我們的教授還沒有討論什麼是模態對話框,所以我不知道如何編程。 – Mariel
[如何製作對話框](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) – MadProgrammer