-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
106 lines (89 loc) · 3.49 KB
/
Main.java
File metadata and controls
106 lines (89 loc) · 3.49 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import java.util.HashMap;
import java.awt.Color;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Main {
public static void main(String[] args) {
HashMap<String, String> acc = readFile();
//jframe
JFrame frame = new JFrame("Sign In");
frame.setSize(558, 412);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.black);
//jpanel
JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBackground(Color.BLACK);
//title lable
JLabel label = new JLabel("Sign In");
label.setFont(new Font("Arial", Font.BOLD, 36));
label.setBounds(220, 20, 200, 40);
panel.add(label);
//logins
//user
JLabel userLabel = new JLabel("Email or mobile number");
userLabel.setBounds(70, 100, 200, 30);
panel.add(userLabel);
JTextField userField = new JTextField();
userField.setBounds(70, 130, 400, 30);
panel.add(userField);
//password
JLabel Passwordlabel = new JLabel("Password");
Passwordlabel.setBounds(70, 170, 200, 30);
panel.add(Passwordlabel);
JPasswordField passField = new JPasswordField();
passField.setBounds(70, 200, 400, 30);
panel.add(passField);
//log in button
JButton button = new JButton("Sign In");
button.setBounds(70, 250, 400, 40);
button.setBackground(Color.RED);
button.setForeground(Color.WHITE);
button.setFont(new Font("Arial", Font.BOLD, 18));
frame.add(button);
frame.add(panel);
frame.setVisible(true);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String userName = userField.getText();
String password = new String(passField.getPassword());
if (acc.containsKey(userName)) {
if (acc.get(userName).equals(password)) {
JOptionPane.showMessageDialog(frame, "Log In Successfully");
}else{
JOptionPane.showMessageDialog(frame, "InCorrect Password", "Error", JOptionPane.ERROR_MESSAGE);
}
}else{
JOptionPane.showMessageDialog(frame, "Invalid username or password.", "Error", JOptionPane.ERROR_MESSAGE);
}
}
});
}
static HashMap<String, String> readFile(){
HashMap<String, String> acc = new HashMap<>();
try (BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\Joseph\\Desktop\\userInfo.txt"))) {
String data;
while ((data = br.readLine())!= null) {
String pass = br.readLine();
if (pass != null) {
acc.put(data, pass);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return acc;
}
}