regroupement bouton dans la classe boutonManager.java
This commit is contained in:
149
projet_linea/src/BoutonsManager.java
Normal file
149
projet_linea/src/BoutonsManager.java
Normal file
@@ -0,0 +1,149 @@
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.List;
|
||||
|
||||
public class BoutonsManager {
|
||||
|
||||
// Méthode pour créer le bouton des scores utilisateur
|
||||
public static JButton creerBoutonScores(JFrame fenetre, int utilisateurId) {
|
||||
JButton boutonScores = new JButton("Voir mes scores");
|
||||
|
||||
// Style
|
||||
boutonScores.setBackground(Color.BLACK);
|
||||
boutonScores.setForeground(Color.WHITE);
|
||||
|
||||
// Position
|
||||
boutonScores.setBounds(300, 20, 160, 30);
|
||||
|
||||
// Action au clic
|
||||
boutonScores.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
List<Integer> scores = GestionBDD.recupererScoresUtilisateur(utilisateurId);
|
||||
int meilleurScore = GestionBDD.recupererMeilleurScoreUtilisateur(utilisateurId);
|
||||
|
||||
StringBuilder message = new StringBuilder();
|
||||
message.append("Meilleur score : ").append(meilleurScore).append("\n\n");
|
||||
message.append("Tous les scores :\n");
|
||||
|
||||
for (int score : scores) {
|
||||
message.append(score).append("\n");
|
||||
}
|
||||
|
||||
JOptionPane.showMessageDialog(
|
||||
fenetre,
|
||||
message.toString(),
|
||||
"Mes Scores",
|
||||
JOptionPane.INFORMATION_MESSAGE
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
return boutonScores;
|
||||
}
|
||||
|
||||
// Méthode pour créer le bouton de connexion
|
||||
public static JButton creerBoutonConnexion(JTextField userField, JPasswordField passField, JFrame cadre) {
|
||||
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);
|
||||
|
||||
loginBtn.addActionListener(e -> {
|
||||
String user = userField.getText();
|
||||
String pass = new String(passField.getPassword());
|
||||
int userId = GestionBDD.verifierConnexion(user, pass);
|
||||
|
||||
if (userId != -1) {
|
||||
cadre.dispose();
|
||||
Jeu jeu = new Jeu(userId);
|
||||
jeu.demarrer();
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(cadre, "Acces refuse");
|
||||
}
|
||||
});
|
||||
|
||||
return loginBtn;
|
||||
}
|
||||
|
||||
// Méthode pour créer le bouton "Créer un compte"
|
||||
public static JButton creerBoutonCreerCompte(JFrame cadre) {
|
||||
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);
|
||||
|
||||
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 = creerBoutonCreerCompteFinal(newUserField, newPassField, createFrame);
|
||||
|
||||
createBox.add(createTitre);
|
||||
createBox.add(Box.createVerticalStrut(30));
|
||||
createBox.add(new JLabel("<html><font color='white'>Nouveau 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);
|
||||
createFrame.setVisible(true);
|
||||
});
|
||||
|
||||
return createBtn;
|
||||
}
|
||||
|
||||
// Méthode pour créer le bouton "Créer" dans la fenêtre de création de compte
|
||||
private static JButton creerBoutonCreerCompteFinal(JTextField newUserField, JPasswordField newPassField, JFrame createFrame) {
|
||||
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);
|
||||
|
||||
createAccountBtn.addActionListener(e -> {
|
||||
String newUser = newUserField.getText();
|
||||
String newPass = new String(newPassField.getPassword());
|
||||
|
||||
if (!newUser.isEmpty() && !newPass.isEmpty()) {
|
||||
boolean success = GestionBDD.creerUtilisateur(newUser, newPass);
|
||||
if (success) {
|
||||
JOptionPane.showMessageDialog(createFrame, "Compte créé avec succès !");
|
||||
createFrame.dispose();
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(createFrame, "Erreur lors de la création du compte.");
|
||||
}
|
||||
} else {
|
||||
JOptionPane.showMessageDialog(createFrame, "Veuillez remplir tous les champs.");
|
||||
}
|
||||
});
|
||||
|
||||
return createAccountBtn;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user