Richard Schmidt
11 months ago
3 changed files with 170 additions and 8 deletions
@ -0,0 +1,122 @@ |
|||
import javax.swing.*; |
|||
import java.awt.event.ActionEvent; |
|||
import java.awt.event.ActionListener; |
|||
import java.util.List; |
|||
import java.awt.event.KeyEvent; |
|||
import java.awt.event.KeyListener; |
|||
|
|||
public class LoginGUI extends JFrame implements ActionListener { |
|||
private JTextField usernameField; |
|||
private JPasswordField passwordField; |
|||
private JButton loginButton; |
|||
private JCheckBox stayLoggedInCheckbox; |
|||
|
|||
public LoginGUI() { |
|||
setTitle("Login"); |
|||
setSize(300, 180); |
|||
setDefaultCloseOperation(EXIT_ON_CLOSE); |
|||
setLayout(null); |
|||
|
|||
JLabel usernameLabel = new JLabel("Username:"); |
|||
usernameLabel.setBounds(20, 20, 80, 25); |
|||
add(usernameLabel); |
|||
|
|||
usernameField = new JTextField(); |
|||
usernameField.setBounds(100, 20, 160, 25); |
|||
add(usernameField); |
|||
|
|||
JLabel passwordLabel = new JLabel("Password:"); |
|||
passwordLabel.setBounds(20, 50, 80, 25); |
|||
add(passwordLabel); |
|||
|
|||
passwordField = new JPasswordField(); |
|||
passwordField.setBounds(100, 50, 160, 25); |
|||
add(passwordField); |
|||
|
|||
stayLoggedInCheckbox = new JCheckBox("Stay Logged In"); |
|||
stayLoggedInCheckbox.setBounds(20, 80, 150, 25); |
|||
add(stayLoggedInCheckbox); |
|||
|
|||
loginButton = new JButton("Login"); |
|||
loginButton.setBounds(100, 110, 100, 25); |
|||
loginButton.addActionListener(this); |
|||
add(loginButton); |
|||
|
|||
getRootPane().setDefaultButton(loginButton); |
|||
|
|||
passwordField.addKeyListener(new EnterKeyListener()); |
|||
|
|||
stayLoggedInCheckbox.addActionListener(new ActionListener() { |
|||
@Override |
|||
public void actionPerformed(ActionEvent e) { |
|||
boolean stayLoggedIn = stayLoggedInCheckbox.isSelected(); |
|||
String username = usernameField.getText(); |
|||
updateStayLoggedIn(username, stayLoggedIn); |
|||
} |
|||
}); |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void actionPerformed(ActionEvent e) { |
|||
if (e.getSource() == loginButton) { |
|||
login(); |
|||
} |
|||
} |
|||
|
|||
private void login() { |
|||
String username = usernameField.getText(); |
|||
String password = new String(passwordField.getPassword()); |
|||
boolean stayLoggedIn = stayLoggedInCheckbox.isSelected(); // Get checkbox state |
|||
|
|||
if (authenticateUser(username, password)) { |
|||
JOptionPane.showMessageDialog(this, "Login successful!"); |
|||
// Perform actions after successful login |
|||
|
|||
dispose(); |
|||
} else { |
|||
JOptionPane.showMessageDialog(this, "Invalid username or password", "Login Error", JOptionPane.ERROR_MESSAGE); |
|||
} |
|||
} |
|||
|
|||
private void updateStayLoggedIn(String username, boolean stayLoggedIn) { |
|||
// Update stayLoggedIn in the JSON file for the user |
|||
CreateUser.updateStayLoggedIn("user.json", username, stayLoggedIn); |
|||
} |
|||
|
|||
// Function to authenticate the user by comparing the entered username and password with the saved user data |
|||
private boolean authenticateUser(String username, String password) { |
|||
List<CreateUser> userList = CreateUser.readUserListFromJsonFile("user.json"); |
|||
if (userList != null) { |
|||
for (CreateUser user : userList) { |
|||
if (user.getUserName().equals(username) && user.getPassword().equals(password)) { |
|||
return true; //Success |
|||
} |
|||
} |
|||
} |
|||
return false; // Fail |
|||
} |
|||
|
|||
|
|||
private class EnterKeyListener implements KeyListener { |
|||
@Override |
|||
public void keyTyped(KeyEvent e) {} |
|||
|
|||
@Override |
|||
public void keyPressed(KeyEvent e) { |
|||
if (e.getKeyCode() == KeyEvent.VK_ENTER) { |
|||
login(); |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public void keyReleased(KeyEvent e) {} |
|||
} |
|||
|
|||
public static void main(String[] args) { |
|||
SwingUtilities.invokeLater(() -> { |
|||
LoginGUI loginGUI = new LoginGUI(); |
|||
loginGUI.setVisible(true); |
|||
}); |
|||
} |
|||
} |
@ -1,14 +1,23 @@ |
|||
[ |
|||
{ |
|||
"id": "961ca202-ecbd-4dfc-ac0b-28f367618aa1", |
|||
"userName": "asd", |
|||
"password": "123456", |
|||
"birthday": "1" |
|||
"id": "a2864d79-1079-4cbb-8d77-f5f84995580d", |
|||
"userName": "Another Test User", |
|||
"password": "TestPasswort123", |
|||
"birthday": "01.01.2000", |
|||
"stayLoggedIn": false |
|||
}, |
|||
{ |
|||
"id": "d563a466-753b-4a5e-8b6c-e7e4756c7397", |
|||
"userName": "asd1", |
|||
"id": "3690702d-9c7e-48fb-8a01-ef89b3b76268", |
|||
"userName": "TestUser2", |
|||
"password": "123456", |
|||
"birthday": "1" |
|||
"birthday": "01.01.2000", |
|||
"stayLoggedIn": false |
|||
}, |
|||
{ |
|||
"id": "685bc3a6-e706-4214-a5e1-8443d1a5258e", |
|||
"userName": "Test User", |
|||
"password": "Test", |
|||
"birthday": "01.01.2000", |
|||
"stayLoggedIn": true |
|||
} |
|||
] |
Write
Preview
Loading…
Cancel
Save
Reference in new issue