IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Interfaces Graphiques en Java Discussion :

[DEBUTANT] Comment supprimer un ecouteur dynamiquement?


Sujet :

Interfaces Graphiques en Java

  1. #1
    Membre du Club Avatar de kayzra
    Inscrit en
    Novembre 2006
    Messages
    110
    Détails du profil
    Informations forums :
    Inscription : Novembre 2006
    Messages : 110
    Points : 68
    Points
    68
    Par défaut [DEBUTANT] Comment supprimer un ecouteur dynamiquement?
    Je souhaiterai savoir comment il serait possible d'empecher un ecouteur (mouseListener) de fonctionner apres qu'il est ete utilise une fois.

    Voici un des ecouteurs de mouseClicked sur un de mes boutons que je souhaiterai desactiver aprés 1 clic :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
     
            ...
            jButton4.setForeground(new java.awt.Color(236, 233, 216));
            jButton4.setText(bundle.getString("grille.jButton4.text")); 
            jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    grille.this.mouseClicked(evt);
                }
            });
            jPanel1.add(jButton4);
            ...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    private void mouseClicked(java.awt.event.MouseEvent evt) {                              
            JButton source = (JButton) evt.getSource();
            source.setBackground(Color.blue);
            source.setForeground(Color.blue);
            ...
            }
    Je precise que je dispose de 100 boutons gerés par la methode getSource().
    J'ai essayé la methode setEnabled(false) mais elle ne permet pas de desactiver l'ecouteur.
    Merci d'avance pour votre aide

    De plus j'ai remarqué que parfois sur mes application utilisant des JButtons il est necessaire de clicker a plusieur reprise pour que l'action soit pris en compte! Est-ce que c'est normale ? Quelqu'un pourrait il me dire d'où vient se probleme?

  2. #2
    Expert éminent sénior
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Points : 12 977
    Points
    12 977
    Par défaut
    bah tu as la méthode removeActionListener, mais comme tu utilises une classe anonyme c'est foutu....
    Hey, this is mine. That's mine. All this is mine. I'm claiming all this as mine. Except that bit. I don't want that bit. But all the rest of this is mine. Hey, this has been a really good day. I've eaten five times, I've slept six times, and I've made a lot of things mine. Tomorrow, I'm gonna see if I can't have sex with something.

  3. #3
    Membre expert
    Avatar de ®om
    Profil pro
    Inscrit en
    Janvier 2005
    Messages
    2 815
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2005
    Messages : 2 815
    Points : 3 080
    Points
    3 080
    Par défaut
    Sinon, pour transférer comme tu le fais un évènement, il faut utiliser un dispatch() et pas simplement le transférer comme tu fais...

  4. #4
    Membre confirmé
    Profil pro
    Inscrit en
    Juin 2005
    Messages
    760
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2005
    Messages : 760
    Points : 626
    Points
    626
    Par défaut
    1. tu gardes une reference vers ton listener et tu fais un remove*Listener
    2. tu utilises get*Listener et tu les retires tous
    3. tu utilises un champ boolean (attribut de ton listener) pour n'executer le code qu'une seule fois
    4. tu retire le listener lors de l'execution de celui-ci (et c'est bien ce que tu veux, non ?) (this faisant reference a ton listener, et ton boutton etant la source de ton event...)


    Edit : Et par convention, les noms de classes commencent par une majuscule.

  5. #5
    Membre du Club Avatar de kayzra
    Inscrit en
    Novembre 2006
    Messages
    110
    Détails du profil
    Informations forums :
    Inscription : Novembre 2006
    Messages : 110
    Points : 68
    Points
    68
    Par défaut
    Merci pour vos reponses.
    N'ayant pas trop d'experience j'ai un peu de mal a comprendre vos explications

    Au sujet de la méthode dispatch() comment l'applique t on dans la pratique?.
    j'ai regardé la doc mais ça reste floue pour moi

    Pour solutionner mon probleme je pense utiliser une ArrayList dans laquelle j'introduirai apres chaque clic la source(JButton) et jutiliserai un if afin de zapper l'ecouteur :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    if(maListe.contains(source)==true) break;//si true ne pas appliquer les méthodes
    ce qui ressemble un peu a ce que TabrisLeFol me propose (a mon niveau : c'est à dire pas terrible!!!)

    kayzra a dit:
    De plus j'ai remarqué que parfois sur mes application utilisant des JButtons il est necessaire de clicker a plusieur reprise pour que l'action soit pris en compte! Est-ce que c'est normale ? Quelqu'un pourrait il me dire d'où vient se probleme?
    Quelqu'un aurrait il une explication?

  6. #6
    Membre confirmé
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    548
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juillet 2006
    Messages : 548
    Points : 635
    Points
    635
    Par défaut
    Quelqu'un aurrait il une explication?
    Utilise un ActionListener plutôt qu'un MouseListener/MouseAdapter

  7. #7
    Membre confirmé
    Profil pro
    Inscrit en
    Juin 2005
    Messages
    760
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2005
    Messages : 760
    Points : 626
    Points
    626
    Par défaut
    C'était la solution en gras qui était la mieux.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    MouseListener ml = new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                  ((JButton)evt.getSource()).removeMouseListener((MouseListener)this);
                    grille.this.mouseClicked(evt);
                }
    });
    Apres tu l'utilise pour autant de boutons que tu veux. Au niveau de la gestion des evenements, ce n'est peut etre effectivement pas la meilleure solution.

    Et pour ton problème de reponse, je dirais que tu utilise mal les threads (en particulier l'EDT). Que fait la grille? Et pourquoi, est elle également un mouseLisener ?

    Et oui : Pourquoi un mouseListener pour le bouton?

  8. #8
    Membre du Club Avatar de kayzra
    Inscrit en
    Novembre 2006
    Messages
    110
    Détails du profil
    Informations forums :
    Inscription : Novembre 2006
    Messages : 110
    Points : 68
    Points
    68
    Par défaut
    Il s'agit d'un jeu de touche-coule dont voici un apercu du code j'espere qu'il est comprehensible :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    /*
     * grille.java
     
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class grille extends javax.swing.JFrame {
        
        /** Creates new form grille */
        public grille() {
            initComponents();
            jPanel1.setVisible(false);
            setBounds(250,100,500,500);
            System.out.println("getSize() : " + getSize());
        }
     
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
        private void initComponents() {
     
            jPanel1 = new javax.swing.JPanel();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jButton3 = new javax.swing.JButton();
            jButton4 = new javax.swing.JButton();
            jButton5 = new javax.swing.JButton();
            jButton6 = new javax.swing.JButton();
            jButton7 = new javax.swing.JButton();
            jButton8 = new javax.swing.JButton();
            jButton9 = new javax.swing.JButton();
            jButton10 = new javax.swing.JButton();
            jButton11 = new javax.swing.JButton();
            jButton12 = new javax.swing.JButton();
            ...
            jPanel2 = new javax.swing.JPanel();
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            jLabel4 = new javax.swing.JLabel();
     
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setBackground(new java.awt.Color(255, 204, 204));
            jPanel1.setLayout(new java.awt.GridLayout(10, 10));
     
            jButton1.setForeground(new java.awt.Color(236, 233, 216));
            jButton1.setText(bundle.getString("grille.jButton1.text")); // NOI18N
            jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    grille.this.mouseClicked(evt);
                }
            });
     
            jPanel1.add(jButton1);
     
            jButton2.setForeground(new java.awt.Color(236, 233, 216));
            jButton2.setText(bundle.getString("grille.jButton2.text")); // NOI18N
            jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    grille.this.mouseClicked(evt);
                }
            });
     
            jPanel1.add(jButton2);
     
            jButton3.setForeground(new java.awt.Color(236, 233, 216));
            jButton3.setText(bundle.getString("grille.jButton3.text")); // NOI18N
            jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    grille.this.mouseClicked(evt);
                }
            });
     
            jPanel1.add(jButton3);
     
     
            jLabel1.setFont(new java.awt.Font("Comic Sans MS", 1, 12));
            jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            jLabel1.setText(bundle.getString("grille.jLabel1.text")); // NOI18N
     
            jLabel2.setFont(new java.awt.Font("Comic Sans MS", 1, 12));
            jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            jLabel2.setText(bundle.getString("grille.jLabel2.text")); // NOI18N
     
            jLabel3.setFont(new java.awt.Font("Comic Sans MS", 1, 12));
            jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            jLabel3.setText(bundle.getString("grille.jLabel3.text")); // NOI18N
     
            jLabel4.setFont(new java.awt.Font("Comic Sans MS", 1, 12));
            jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
            jLabel4.setText(bundle.getString("grille.jLabel4.text")); // NOI18N
     
            javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
            jPanel2.setLayout(jPanel2Layout);
            jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 88, Short.MAX_VALUE)
                    .addComponent(jLabel3)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap())
            );
            jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
            );
            getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_END);
     
            pack();
        }// </editor-fold>//GEN-END:initComponents
     
        /*LA GRILLE SE COMPOSE DE 100 BOUTON NUMEROTES DE 0 A 100 POSITIONNER
         * PAR UN GRIDLAYOUT DE 10/10
         *
         * 00 01 02 03 04 05 06 07 08 09
         * 10 11 12 13 14 15 16 17 18 19
         * 20 21 22 23 24 25 26 27 28 29
         * 30 31 32 33 34 35 36 37 38 ...
         *UN BATEAU PEU OCCUPER PAR EXEMPLE SUR 3 CASES EX:11 12 13 HORIZONTALEMENT OU 
         *11 21 31 VERTICALEMENT.
         * LA METHODE PlacementGrille(n)RETOURNE 1 ARRAYLIST, n CORRESPON A LA TAILLE DU BATEAU EN NBRE DE CASES. IL Y A 5 BATEAU :1 PORTAVION(5 CASES horizontalement ou verticalement), 
         *2 DESTROYERS(OCCUPE 3 CASES) ET 2 FREGATES(2 CASES )
    	 *ELLES SONT ENSUITE TOUTES LES 5 PLACEES DANS UNE 6EME ARRAYLIST MEGA SI ELLES NE SE
    	 *CHEVAUCHE PAS*/
    	public void placer(){
                /*REFERENCE OBJET PERMETTANT DE PLACER AU HAZARD UN BATEAU
                 *(RETOURNE LES CASES OU SE TROUVE LE BATEAU SOUS FORME
                 *D'UNE LISTE) DE DIMENSION SPECIFIER SANS QUE CELUI-CI
                 * NE SOIT SUR PLUSIEUR LIGNE OU COLONNE PAR EXEMPLE 19 20 21*/
                PlacementGrille laPlace = new PlacementGrille();
     
     
                 /*POUR CHAQUE NAVIRE ON TEST LES VALEUR DU BATEAU DE
                  * FACONT A CE QU'IL NE SE SUPPERPOSE PAS SUR LES AUTRES NAVIRES
                  * SI != LES VALEURS SONT COPIEES DANS LA LIST MEGA JE SUIS SURE QUE CETTE
                  * FACONT DE FAIRE EST TRES "GAUCHE" MAIS JE N'AI PAS REUSSIE AUTREMENT*/
     
                do{
                	MEGA.clear();portAvion.clear();destroyer1.clear();
                	//INITIALISE  0 EN CAS DE SUPPERPOSITION
                	portAvion.addAll(laPlace.Placement(5));
                	/*LE PORTAVION N'EST PAS TESTE VUE QUE LA GRILLE
                	 * EST VIDE IL NE PEU SE SUPPERPOSER SUR UN AUTRE*/
                	MEGA.addAll(portAvion);//IL EST DONC AUTOMATIQUEMENT INCLUS DANS MEGA
                	destroyer1.addAll(laPlace.Placement(3));
                	d1 = destroyer1.get(0);//CORRESPOND INDICE 0 DE  LA ARRAYLIST DESTROYER1
      				d2 = destroyer1.get(1);//"        "        1          "          "
      				d3 = destroyer1.get(2);//"        "        2          "          "
                }while(MEGA.contains(d1) == true//TEST LES VALEUR DU BATEAU DE
                        | MEGA.contains(d2) == true//FACONT A CE QU'IL NE SE SUPPERPOSE
                        | MEGA.contains(d3) == true);// PAS SUR LES AUTRES NAVIRES
                MEGA.addAll(destroyer1);//SI != LES VALEURS SONT COPI DANS LA LIST MEGA
                do{
    				destroyer2.clear();
    				destroyer2.addAll(laPlace.Placement(3));
    				d4 = destroyer2.get(0);
    				d5 = destroyer2.get(1);
    				d6 = destroyer2.get(2);
                }while(MEGA.contains(d4) == true
                        | MEGA.contains(d5) == true
                        | MEGA.contains(d6) == true);
                MEGA.addAll(destroyer2);
                do{
                	fregate1.clear();
                	fregate1.addAll(laPlace.Placement(2));
                	f1 = fregate1.get(0);
                	f2 = fregate1.get(1);
                }while(MEGA.contains(f1) == true | MEGA.contains(f2) == true);
                MEGA.addAll(fregate1);
                do{
                	fregate2.clear();
                	fregate2.addAll(laPlace.Placement(2));
                	f3 = fregate2.get(0);
      				f4 = fregate2.get(1);
                }while(MEGA.contains(f3) == true | MEGA.contains(f4) == true);
                MEGA.addAll(fregate2);
     
                for(int i=0;i<MEGA.size();i++)System.out.println("en "+i+" : "+MEGA.get(i));//affiches toutes les position des bateau
                System.out.println("MEGA.size(): " +  MEGA.size());//affiche la taille de MEGA
    	}
     
            public void setReste() {
                reste = reste - 5;
            }
    //§§§§§§§§§§§§§§ SE BOUTON LANCE UNE NOUVELLE GRILLE§§§§§§§§§§§§§§
        private void jButton100ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton100ActionPerformed
     
            pan.setVisible(false);
            MEGA.clear();//-
            portAvion.clear();//-
            destroyer1.clear();//INITIALISE LES ARRAYLIST
            destroyer2.clear();//-
            fregate1.clear();//-
            fregate2.clear();//-
            jPanel1.setVisible(false);//-1-( & -2-) reinitialise l'AFFICHAGE DE GRILLE
            initComponents();
            placer();/*PLACE LES POSITION(CASES) OCCUPER PAR LES BATEAU  DANS LES ARRAYLIST*/
     
            setBounds(250,100,500,500);
            jPanel1.setVisible(true);//-2-
            System.out.println("getSize() : " + getSize());
            difficulte+=2;//AUGMENT DIFFICULTE PAR DIMINUTION DU NBRE D'ESSAI
            clic = 0;//DONNE LE NBRE DE TENTATIVES
            reste = 62;//DONNE LE NBRES DE TENTATIVES RESTANTES
            reste =( reste - difficulte);//CALCUL TENTATIVE AUTORISEE
            jLabel4.setText(restant=Integer.toString(reste));/*AFFICHE LE NBRE DE TENTATIVES RESTANTE*/
     
        }//GEN-LAST:event_jButton100ActionPerformed
     
    //§§§§§§§§§§LES CLIC SUR LES BOUTONS DE LA GRILLE§§§§§§§§§
     
        private void mouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mouseClicked
            clic++;
            reste--;
     
            jLabel2.setText(essai=Integer.toString(clic));
            jLabel4.setText(restant=Integer.toString(reste));
            if(reste == 0) {/*SI LE NOMBRE D'ESSAI AUTORISES EST EGALE A 0
    *C4EST PERDUE ET ON AFFICHE DANS UN AUTRE PANEL "PERDU"*/
                jPanel1.setVisible(false);
                pan.setBackground(Color.green);
                pan.setSize(500,500);
                add(pan);
                javax.swing.JLabel jLabelVictoire = new JLabel();
                jLabelVictoire.setText("!!! PERDU !!!");
                jLabelVictoire.setFont(new java.awt.Font("Comic Sans MS", 1, 48));
                jLabelVictoire.setForeground(Color.RED);
                jLabelVictoire.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
                jLabelVictoire.setVerticalAlignment(javax.swing.SwingConstants.CENTER);
                pan.add(jLabelVictoire);
                pan.setVisible(true);
                pan.validate();
                restant = "";
                essai = "";
            }
     
            /*LE BOUTON DEVIENT BLEU SAUF SI SON getText() == A UNE VALEUR
     *CONTENUS DANS LA LISTE MEGA (QUI REGROUPE LES POSITION DE TOUT
     *LES BATEAU) DANS CE CAS LE JBUTTON DEVIENT ROUGE*/
     
            JButton source = (JButton) evt.getSource();
            source.setBackground(Color.blue);
            source.setForeground(Color.blue);
            //source.setEnabled(false);
     
            String nom = source.getText();
            System.out.println("clic button numero : " + nom);
            for(int f = 0 ; f <= MEGA.size()-1 ; f++)
        	{
        	//System.out.println("TAILLE DE MAGA : "+MEGA.size());
        	int val = Integer.parseInt(nom);
        	if(val == MEGA.get(f)){
                    reste++;
        		source.setBackground(Color.red);
        		source.setForeground(Color.red);
                    //source.setEnabled(false);
        		//System.out.println("val : "+val + "---" + "f : "+MEGA.get(f)+ "source : " + source);
                    MEGA.remove(f);
                    System.out.println( f + " a ete effacer!!!" + "MEGA.size() : " + MEGA.size());
     
                    if(MEGA.size()== 0) {/* LES VALEUR DE MEGA ETANT EFFACEES 
    *AU FUR ET A MESURE QUELLE SONT DECOUVERTE SI MEGA EST EGALE A 0 
    *C'EST GAGNE ET ON AFFICHE DANS UN AUTRE PANEL "VICTOIRE"*/
                        jPanel1.setVisible(false);
                        pan.setBackground(Color.red);
                        pan.setSize(500,500);
                        add(pan);
                        javax.swing.JLabel jLabelVictoire = new JLabel();
                        jLabelVictoire.setText("!!! VICTOIRE !!!");
                        jLabelVictoire.setFont(new java.awt.Font("Comic Sans MS", 1, 48));
                        jLabelVictoire.setForeground(Color.YELLOW);
                        jLabelVictoire.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
                        jLabelVictoire.setVerticalAlignment(javax.swing.SwingConstants.CENTER);
                        pan.add(jLabelVictoire);
                        pan.setVisible(true);
                        pan.validate();
     
                    }
        		}
    		}
        }//GEN-LAST:event_mouseClicked
     
     
     
     
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new grille().setVisible(true);
     
                }
     
            });
         //Scanner lecture = new Scanner(System.in);
         //int x = lecture.nextInt();
         //if(x == 0) System.exit(1);
        }
     
        private ArrayList<Integer> portAvion = new ArrayList<Integer>();//represente 5 case
        private ArrayList<Integer> destroyer1 = new ArrayList<Integer>();//represente 3 cases
        private ArrayList<Integer> destroyer2 = new ArrayList<Integer>();//represente 3 cases
        private ArrayList<Integer> fregate1 = new ArrayList<Integer>();//represente 2 case
        private ArrayList<Integer> fregate2 = new ArrayList<Integer>();//represente 2 case
     
        private ArrayList<Integer> MEGA = new ArrayList<Integer>();//LISTE QUI REGROUPE LES DONNES DES AUTRES LISTES
     
        private int p1,p2,p3,p4,p5;//VALEUR OU SE TROUVE LE PORTAVION PERMET DE TESTER LA SUPPERPOSITION
        private int d1,d2,d3,d4,d5,d6;//VALEUR OU SE TROUVE LES 2 DESTROYERS
        private int f1,f2,f3,f4;//VALEUR DOU SE TROUVE LES 2 FREGATES
     
        private boolean test;//INUTILISE POUR L'INSTANT SERVIRA POUR UNE FUTUR I.A
     
        private Integer clic;//DONNE LE NOMBRE DE TENTATIVES
        private Integer reste = 60;//DONNE LE NOMBRE DE TENTATIVES RESTANTES
     
        private String restant;
        private String essai;
     
        private int difficulte ;//BAISSE LE NOMBRE DE TENTATIVES AUTORISEES A CHAQUE PARTIE
        private javax.swing.JPanel pan = new JPanel();//SERT A AFFICHER UN PAR JLABEL GAGNE OU PERDU
     
        // Variables declaration - do not modify//GEN-BEGIN:variables
        private javax.swing.JButton jButton1;
        ...
        private javax.swing.JButton jButton99;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JPanel jPanel1;//la grille
        private javax.swing.JPanel jPanel2;//affiche la victoire ou la defaite
        // End of variables declaration//GEN-END:variables
     
    }
    Mon probleme vient du fait que lorsque je clic sur un bouton et qu'il devient rouge si je reclique dessus il devient bleu puisqu'il a ete supprimer de la liste MEGA au 1er clic.En effet si la valeur de jButton.getText() ne se trouve pas de l'ArrayList celci devient bleu.Je recherchai methode simple applicable a mon aplication du type removeListener() que j'avais deja tante d'appliquer sans succés.

    je suis toujours interessé par la methode dispatch() que je ne comprend pas.

    Quand à MouseClicked je ne savais pas qu'il vallait mieu utiliser un ActionPerformed c'est le probleme quand on se forme par soit même : on a personne pour corriger ses erreurs

    Merci en tout cas a tous pour votre aide.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 11
    Dernier message: 25/03/2013, 11h06
  2. [VB6]comment supprimer dynamiquement un controle dynamique?
    Par totor le troll dans le forum VB 6 et antérieur
    Réponses: 9
    Dernier message: 06/07/2011, 21h10
  3. comment supprimer une lettre en debut du liste de mot?
    Par doogybreton dans le forum Excel
    Réponses: 2
    Dernier message: 10/12/2007, 00h37
  4. Réponses: 2
    Dernier message: 16/06/2006, 22h07
  5. comment creer un alias dynamique avec BDE et ODBC
    Par david33 dans le forum C++Builder
    Réponses: 2
    Dernier message: 12/07/2002, 11h50

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo