This repository was archived by the owner on Jul 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathForgotPasswordApp.java
More file actions
65 lines (49 loc) · 1.96 KB
/
ForgotPasswordApp.java
File metadata and controls
65 lines (49 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// File: ForgotPasswordApp.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ForgotPasswordApp extends JFrame implements ActionListener {
private JLabel securityQuestionLabel;
private JTextField securityAnswerField;
private JButton retrievePasswordButton;
private User user;
public ForgotPasswordApp(User user) {
this.user = user;
setTitle("Forgot Password");
setLayout(new BorderLayout(20, 20));
JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
securityQuestionLabel = new JLabel("Security Question: " + user.securityQuestion);
mainPanel.add(securityQuestionLabel, BorderLayout.NORTH);
securityAnswerField = new JTextField();
mainPanel.add(securityAnswerField, BorderLayout.CENTER);
retrievePasswordButton = new JButton("Retrieve Password");
retrievePasswordButton.addActionListener(this);
mainPanel.add(retrievePasswordButton, BorderLayout.SOUTH);
add(mainPanel);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == retrievePasswordButton) {
retrievePassword();
}
}
private void retrievePassword() {
String answer = securityAnswerField.getText();
if (answer.equals(user.securityAnswer)) {
JOptionPane.showMessageDialog(this, "Your password is: " + new String(user.password));
dispose();
} else {
JOptionPane.showMessageDialog(this, "Incorrect answer to security question.");
}
}
public static void main(String[] args) {
User testUser = new User("testuser", "testpassword".toCharArray(), "What is your favorite color?", "Green");
SwingUtilities.invokeLater(() -> {
new ForgotPasswordApp(testUser);
});
}
}