fin du cotrole blanc dev32

This commit is contained in:
2023-12-19 19:50:02 +01:00
parent 1fc92e50c3
commit b1a83ffc24
15 changed files with 133 additions and 66 deletions

View File

@@ -1,15 +0,0 @@
import javax.swing.JCheckBox;
public class Coche extends JCheckBox{
private Ingredient valeur;
public Coche(Ingredient valeur){
super(valeur.name());
this.valeur = valeur;
}
public Ingredient getValeur(){
return this.valeur;
}
}

View File

@@ -1,21 +1,22 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Deque;
public class EvenementIngredient implements ItemListener{
private PileIngredient historiqueChoix;
private Deque<JCheckBox> historiqueChoix;
private JButton boutonRetour;
public EvenementIngredient(PileIngredient historiqueChoix, JButton boutonRetour){
public EvenementIngredient(Deque<JCheckBox> historiqueChoix, JButton boutonRetour){
this.historiqueChoix = historiqueChoix;
this.boutonRetour = boutonRetour;
}
@Override
public void itemStateChanged(ItemEvent e){
Coche coche = (Coche) e.getSource();
this.historiqueChoix.push(coche.getValeur());
JCheckBox coche = (JCheckBox) e.getSource();
this.historiqueChoix.push(coche);
this.boutonRetour.setEnabled(true);
}
}

View File

@@ -1,33 +1,25 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Deque;
public class EvenementRetour implements ActionListener{
private PileIngredient historique;
private Coche[] listeCoche;
private EvenementIngredient evenementIngredient;
private Deque<JCheckBox> historique;
public EvenementRetour(PileIngredient historique, Coche[] listeCoche, EvenementIngredient evenementIngredient){
public EvenementRetour(Deque<JCheckBox> historique){
this.historique = historique;
this.listeCoche = listeCoche;
this.evenementIngredient = evenementIngredient;
}
@Override
public void actionPerformed(ActionEvent e){
Ingredient dernierChoisis = this.historique.pop();
JCheckBox dernierChoisis = this.historique.pop();
JButton boutonRetour = (JButton) e.getSource();
for (Coche coche : this.listeCoche){
if (coche.getValeur() == dernierChoisis){
coche.removeItemListener(evenementIngredient);
coche.setSelected(!coche.isSelected());
coche.addItemListener(evenementIngredient);
if (historique.isEmpty()){
boutonRetour.setEnabled(false);
}
}
dernierChoisis.setSelected(!dernierChoisis.isSelected()); // cette ligne declenche l'evenementIngredient
this.historique.pop(); // il faut donc retirer le dernier ingredient ajouter malencontreusement
if (historique.isEmpty()){
boutonRetour.setEnabled(false);
}
}
}

View File

@@ -1,9 +1,11 @@
import javax.swing.*;
import java.awt.*;
import java.util.Deque;
import java.util.ArrayDeque;
public class Fenetre extends JFrame{
private PileIngredient historique;
private Deque<JCheckBox> historique;
public Fenetre(){
super();
@@ -11,7 +13,7 @@ public class Fenetre extends JFrame{
this.setSize(500, 300);
this.setLocation(0, 0);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.historique = new PileIngredient();
this.historique = new ArrayDeque<>();
this.addIngredient();
}
@@ -20,17 +22,17 @@ public class Fenetre extends JFrame{
EvenementIngredient evenementIngredient = new EvenementIngredient(this.historique, retour);
Ingredient[] listeIngredient = Ingredient.values();
int nbIngredient = listeIngredient.length;
Coche[] listeCoche = new Coche[nbIngredient];
JCheckBox[] listeCoche = new JCheckBox[nbIngredient];
int i;
this.setLayout(new GridLayout(1,nbIngredient+1));
this.setLayout(new GridLayout(nbIngredient+1,1));
for (i=0; i<nbIngredient; i++){
listeCoche[i] = new Coche(listeIngredient[i]);
listeCoche[i] = new JCheckBox(listeIngredient[i].name());
listeCoche[i].addItemListener(evenementIngredient);
this.add(listeCoche[i]);
}
EvenementRetour evenementRetour = new EvenementRetour(this.historique, listeCoche, evenementIngredient);
EvenementRetour evenementRetour = new EvenementRetour(this.historique);
retour.setEnabled(false);
retour.addActionListener(evenementRetour);
this.add(retour);

View File

@@ -1,5 +1,5 @@
enum Ingredient{
SALADE,
TOMATES,
OIGNONS
OIGNONS,
}

View File

@@ -2,7 +2,7 @@ import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Q4Main{
public class Main{
public static void main(String[] args) {
Fenetre fenetre = new Fenetre();

View File

@@ -1,23 +0,0 @@
import java.util.*;
public class PileIngredient{
private Deque<Ingredient> pile;
public PileIngredient(){
this.pile = new ArrayDeque<>();
}
public void push(Ingredient valeur){
this.pile.push(valeur);
}
public Ingredient pop(){
return this.pile.pop();
}
public boolean isEmpty(){
return this.pile.isEmpty();
}
}