From ee86e1e7429a6713a7bf65c4cc56cd26afe8d68c Mon Sep 17 00:00:00 2001 From: Gw3nd4l Date: Wed, 11 Mar 2026 17:19:03 +0100 Subject: [PATCH] menu login --- src/linea/MenuLogin.java | 99 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/linea/MenuLogin.java diff --git a/src/linea/MenuLogin.java b/src/linea/MenuLogin.java new file mode 100644 index 0000000..453952f --- /dev/null +++ b/src/linea/MenuLogin.java @@ -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; + } +} \ No newline at end of file