menu login

This commit is contained in:
2026-03-11 17:19:03 +01:00
parent eb1ecdf405
commit ee86e1e742

99
src/linea/MenuLogin.java Normal file
View File

@@ -0,0 +1,99 @@
package linea;
import javax.swing.*;
import java.awt.*;
public class MenuLogin extends JPanel {
public MenuLogin(Jeu jeu) {
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
setBackground(new Color(40, 40, 40));
// Titre
JLabel titre = new JLabel("AUTHENTIFICATION");
titre.setForeground(Color.WHITE);
titre.setFont(new Font("SansSerif", Font.BOLD, 42));
titre.setAlignmentX(Component.CENTER_ALIGNMENT);
// Champs de saisie
JTextField identifiantField = new JTextField();
identifiantField.setFont(new Font("SansSerif", Font.PLAIN, 16));
JPasswordField motDePasseField = new JPasswordField();
motDePasseField.setFont(new Font("SansSerif", Font.PLAIN, 16));
// Panneau pour les champs
JPanel fieldsPanel = new JPanel(new GridLayout(2, 2, 10, 10));
fieldsPanel.setOpaque(false);
fieldsPanel.setMaximumSize(new Dimension(350, 80));
fieldsPanel.add(new JLabel("Identifiant :") {{ setForeground(Color.WHITE); setFont(new Font("SansSerif", Font.BOLD, 16)); }});
fieldsPanel.add(identifiantField);
fieldsPanel.add(new JLabel("Mot de passe :") {{ setForeground(Color.WHITE); setFont(new Font("SansSerif", Font.BOLD, 16)); }});
fieldsPanel.add(motDePasseField);
// Boutons
JButton btnLogin = creerBouton("Se connecter");
JButton btnSignup = creerBouton("Créer un compte");
JButton btnQuit = creerBouton("Quitter");
// Action pour se connecter
btnLogin.addActionListener(e -> {
String identifiant = identifiantField.getText();
String motDePasse = new String(motDePasseField.getPassword());
if (identifiant.isEmpty() || motDePasse.isEmpty()) {
JOptionPane.showMessageDialog(this, "Veuillez remplir tous les champs.", "Erreur", JOptionPane.ERROR_MESSAGE);
return;
}
if (jeu.bdd.verifierUtilisateur(identifiant, motDePasse)) {
JOptionPane.showMessageDialog(this, "Connexion réussie ! Bienvenue " + identifiant + ".");
jeu.afficherMenuPrincipal();
} else {
JOptionPane.showMessageDialog(this, "Identifiant ou mot de passe incorrect.", "Erreur de connexion", JOptionPane.ERROR_MESSAGE);
}
});
// Action pour créer un compte
btnSignup.addActionListener(e -> {
String identifiant = identifiantField.getText();
String motDePasse = new String(motDePasseField.getPassword());
if (identifiant.isEmpty() || motDePasse.isEmpty()) {
JOptionPane.showMessageDialog(this, "Veuillez choisir un identifiant et un mot de passe.", "Erreur", JOptionPane.ERROR_MESSAGE);
return;
}
if (jeu.bdd.creerCompte(identifiant, motDePasse)) {
JOptionPane.showMessageDialog(this, "Compte créé avec succès ! Vous pouvez maintenant vous connecter.");
} else {
JOptionPane.showMessageDialog(this, "Cet identifiant est déjà pris ou une erreur est survenue.", "Erreur", JOptionPane.ERROR_MESSAGE);
}
});
// Action pour quitter
btnQuit.addActionListener(e -> {
jeu.bdd.fermerConnexion();
System.exit(0);
});
// Ajout des composants
add(Box.createVerticalGlue());
add(titre);
add(Box.createRigidArea(new Dimension(0, 40)));
add(fieldsPanel);
add(Box.createRigidArea(new Dimension(0, 20)));
add(btnLogin);
add(Box.createRigidArea(new Dimension(0, 10)));
add(btnSignup);
add(Box.createRigidArea(new Dimension(0, 30)));
add(btnQuit);
add(Box.createVerticalGlue());
}
private JButton creerBouton(String texte) {
JButton b = new JButton(texte);
b.setFont(new Font("SansSerif", Font.PLAIN, 18));
b.setAlignmentX(Component.CENTER_ALIGNMENT);
b.setMaximumSize(new Dimension(220, 40));
b.setFocusable(false);
return b;
}
}