bdd connexion utilisateur
This commit is contained in:
138
projet_linea/src/CadreDeConnexion.java
Normal file
138
projet_linea/src/CadreDeConnexion.java
Normal file
@@ -0,0 +1,138 @@
|
||||
import java.awt.*;
|
||||
import javax.swing.*;
|
||||
|
||||
public class CadreDeConnexion extends JFrame {
|
||||
|
||||
public CadreDeConnexion() {
|
||||
// 1. Configuration de base
|
||||
setTitle("Connexion Linea");
|
||||
setSize(800, 600);
|
||||
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||
setLocationRelativeTo(null); // Centre la fenêtre
|
||||
|
||||
// 2. Style du panneau principal (Gris foncé)
|
||||
JPanel panel = new JPanel(new GridBagLayout());
|
||||
panel.setBackground(new Color(30, 30, 30));
|
||||
add(panel);
|
||||
|
||||
// Conteneur pour les éléments (pour les empiler verticalement)
|
||||
Box box = Box.createVerticalBox();
|
||||
|
||||
// 3. Création des composants
|
||||
JLabel titre = new JLabel("CONNEXION");
|
||||
titre.setForeground(Color.WHITE);
|
||||
titre.setFont(new Font("Arial", Font.BOLD, 24));
|
||||
titre.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
|
||||
JTextField userField = new JTextField(15);
|
||||
userField.setMaximumSize(new Dimension(250, 30));
|
||||
|
||||
JPasswordField passField = new JPasswordField(15);
|
||||
passField.setMaximumSize(new Dimension(250, 30));
|
||||
|
||||
JButton loginBtn = new JButton("Entrer");
|
||||
loginBtn.setBackground(new Color(70, 130, 180)); // Bleu acier
|
||||
loginBtn.setForeground(Color.WHITE);
|
||||
loginBtn.setFocusPainted(false);
|
||||
loginBtn.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
|
||||
// 4. Ajout des composants avec des espaces (Struts)
|
||||
box.add(titre);
|
||||
box.add(Box.createVerticalStrut(30)); // Espace
|
||||
box.add(new JLabel("<html><font color='white'>Utilisateur :</font></html>"));
|
||||
box.add(userField);
|
||||
box.add(Box.createVerticalStrut(15));
|
||||
box.add(new JLabel("<html><font color='white'>Mot de passe :</font></html>"));
|
||||
box.add(passField);
|
||||
box.add(Box.createVerticalStrut(30));
|
||||
box.add(loginBtn);
|
||||
box.add(Box.createVerticalStrut(10));
|
||||
|
||||
JButton createBtn = new JButton("Créer un compte");
|
||||
createBtn.setBackground(new Color(70, 130, 180));
|
||||
createBtn.setForeground(Color.WHITE);
|
||||
createBtn.setFocusPainted(false);
|
||||
createBtn.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
box.add(createBtn);
|
||||
|
||||
panel.add(box); // Ajoute la boîte au centre du GridBagLayout
|
||||
|
||||
// 5. Logique du bouton
|
||||
loginBtn.addActionListener(e -> {
|
||||
String user = userField.getText();
|
||||
String pass = new String(passField.getPassword());
|
||||
int userId = GestionBDD.verifierConnexion(user, pass);
|
||||
|
||||
if (userId != -1) {
|
||||
dispose();
|
||||
Jeu jeu = new Jeu(userId);
|
||||
jeu.demarrer();
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(this, "Acces refuse");
|
||||
}
|
||||
});
|
||||
|
||||
createBtn.addActionListener(evt -> {
|
||||
JFrame createFrame = new JFrame("Creation de compte");
|
||||
createFrame.setSize(800, 600);
|
||||
createFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
||||
createFrame.setLocationRelativeTo(null);
|
||||
|
||||
JPanel createPanel = new JPanel(new GridBagLayout());
|
||||
createPanel.setBackground(new Color(30, 30, 30));
|
||||
createFrame.add(createPanel);
|
||||
|
||||
Box createBox = Box.createVerticalBox();
|
||||
|
||||
JLabel createTitre = new JLabel("CREATION DE COMPTE");
|
||||
createTitre.setForeground(Color.WHITE);
|
||||
createTitre.setFont(new Font("Arial", Font.BOLD, 24));
|
||||
createTitre.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
|
||||
JTextField newUserField = new JTextField(15);
|
||||
newUserField.setMaximumSize(new Dimension(250, 30));
|
||||
|
||||
JPasswordField newPassField = new JPasswordField(15);
|
||||
newPassField.setMaximumSize(new Dimension(250, 30));
|
||||
|
||||
JButton createAccountBtn = new JButton("Créer");
|
||||
createAccountBtn.setBackground(new Color(70, 130, 180));
|
||||
createAccountBtn.setForeground(Color.WHITE);
|
||||
createAccountBtn.setFocusPainted(false);
|
||||
createAccountBtn.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
|
||||
createBox.add(createTitre);
|
||||
createBox.add(Box.createVerticalStrut(30));
|
||||
createBox.add(new JLabel("<html><font color='white'>Nouvel utilisateur :</font></html>"));
|
||||
createBox.add(newUserField);
|
||||
createBox.add(Box.createVerticalStrut(15));
|
||||
createBox.add(new JLabel("<html><font color='white'>Mot de passe :</font></html>"));
|
||||
createBox.add(newPassField);
|
||||
createBox.add(Box.createVerticalStrut(30));
|
||||
createBox.add(createAccountBtn);
|
||||
|
||||
createPanel.add(createBox);
|
||||
|
||||
createAccountBtn.addActionListener(e -> {
|
||||
String u = newUserField.getText().trim();
|
||||
String p = new String(newPassField.getPassword());
|
||||
if (u.isEmpty() || p.isEmpty()) {
|
||||
JOptionPane.showMessageDialog(createFrame, "Utilisateur et mot de passe requis");
|
||||
return;
|
||||
}
|
||||
|
||||
boolean ok = GestionBDD.creerUtilisateur(u, p);
|
||||
if (ok) {
|
||||
JOptionPane.showMessageDialog(createFrame, "Compte cree avec succes.");
|
||||
createFrame.dispose();
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(createFrame, "Échec : le nom d'utilisateur existe deja ou erreur.");
|
||||
}
|
||||
});
|
||||
|
||||
createFrame.setVisible(true);
|
||||
});
|
||||
|
||||
setVisible(true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user