forked from menault/TD3_DEV51_Qualite_Algo
76 lines
2.5 KiB
Java
76 lines
2.5 KiB
Java
import javax.swing.*;
|
|
import java.awt.*;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionListener;
|
|
import java.util.function.Consumer;
|
|
|
|
/**
|
|
* Fenêtre de sélection de la difficulté (Facile / Moyen / Difficile).
|
|
* Notifie le choix via un Consumer<String> fourni au constructeur.
|
|
*
|
|
* @version 1.0
|
|
* @author Adrien
|
|
* Date : 08-10-2025
|
|
* Licence :
|
|
*/
|
|
public class MenuDifficulte implements ActionListener {
|
|
|
|
private final JFrame frame;
|
|
private final Consumer<String> onDifficultyChosen;
|
|
|
|
/** Construit la fenêtre et prépare les boutons. */
|
|
public MenuDifficulte(Consumer<String> onDifficultyChosen) {
|
|
this.onDifficultyChosen = onDifficultyChosen;
|
|
|
|
frame = new JFrame("Jeu du Pendu — Choix de difficulté");
|
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
frame.setSize(400, 250);
|
|
frame.setLocationRelativeTo(null);
|
|
frame.setLayout(new BorderLayout(0, 10));
|
|
|
|
JLabel title = new JLabel("Choisissez une difficulté", SwingConstants.CENTER);
|
|
title.setFont(new Font("Segoe UI", Font.BOLD, 18));
|
|
title.setBorder(BorderFactory.createEmptyBorder(20, 10, 0, 10));
|
|
frame.add(title, BorderLayout.NORTH);
|
|
|
|
JPanel buttonsPanel = new JPanel(new GridLayout(1, 3, 10, 10));
|
|
buttonsPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
|
|
|
|
JButton easyBtn = new JButton("Facile");
|
|
JButton mediumBtn = new JButton("Moyen");
|
|
JButton hardBtn = new JButton("Difficile");
|
|
|
|
easyBtn.setActionCommand("Facile");
|
|
mediumBtn.setActionCommand("Moyen");
|
|
hardBtn.setActionCommand("Difficile");
|
|
|
|
easyBtn.addActionListener(this);
|
|
mediumBtn.addActionListener(this);
|
|
hardBtn.addActionListener(this);
|
|
|
|
buttonsPanel.add(easyBtn);
|
|
buttonsPanel.add(mediumBtn);
|
|
buttonsPanel.add(hardBtn);
|
|
frame.add(buttonsPanel, BorderLayout.CENTER);
|
|
}
|
|
|
|
/** Affiche la fenêtre. */
|
|
public void show() { frame.setVisible(true); }
|
|
|
|
/** Ferme la fenêtre. */
|
|
public void close() { frame.dispose(); }
|
|
|
|
/** Accès optionnel au JFrame. */
|
|
public JFrame getFrame() { return frame; }
|
|
|
|
/** Réception des clics sur les boutons. */
|
|
@Override
|
|
public void actionPerformed(ActionEvent actionEvent) {
|
|
String difficulty = actionEvent.getActionCommand(); // "Facile" | "Moyen" | "Difficile"
|
|
frame.dispose();
|
|
if (onDifficultyChosen != null) {
|
|
onDifficultyChosen.accept(difficulty);
|
|
}
|
|
}
|
|
}
|