Browse Source

Made it so pressing enter on the keyboard activates the login and ok button

remotes/origin/server
Richard Schmidt 11 months ago
parent
commit
13d0a13392
  1. 37
      src/main/java/LoginGUI.java

37
src/main/java/LoginGUI.java

@ -2,6 +2,8 @@ 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;
@ -35,22 +37,26 @@ public class LoginGUI extends JFrame implements ActionListener {
loginButton.addActionListener(this);
add(loginButton);
getRootPane().setDefaultButton(loginButton);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == loginButton) {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
if (e.getSource() == loginButton) {
login();
}
}
if (authenticateUser(username, password)) {
private void login() {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
if (authenticateUser(username, password)) {
JOptionPane.showMessageDialog(this, "Login successful!");
// Perform actions after successful login
} else {
} else {
JOptionPane.showMessageDialog(this, "Invalid username or password", "Login Error", JOptionPane.ERROR_MESSAGE);
}
}
}
@ -68,6 +74,21 @@ public class LoginGUI extends JFrame implements ActionListener {
}
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();

Loading…
Cancel
Save