Bonjour a tous,

Mon problème est que lorsque je démarre en appuyant sur une touche de mon clavier (les flèches par exemple) un deuxième JLabel apparaît sans que je le veuille selon l'ordinateur. J'ai essayer mon application sur un mac, cela marche, sur un ordinateur à tour, cela marche aussi(avec Windows XP) mais sur les PC cela marche pas.

Pouvez vous m'aider s'il vous plait!!

Voici mon code :

Nom : bug.PNG
Affichages : 212
Taille : 28,2 Ko

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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SnakeV2 extends JFrame implements KeyListener{
    private JPanel fond, zoneBouton, zoneDeplacement, zoneTitre;
    private JButton droite, haut, bas, gauche, saut;
    private JLabel titre;
    private SerpentV2 serpent;
    public SnakeV2(){
        super("Course de carre");
        fond = new JPanel(new BorderLayout());
        fond.setBackground(Color.BLUE);
 
        //titre
        titre = new JLabel("Course de carre");
        zoneTitre = new JPanel();
 
 
        zoneTitre.add(titre);
 
        fond.add(zoneTitre, BorderLayout.NORTH);
 
        Font font = new Font("Arial",Font.BOLD,50);
        titre.setFont(font);
 
        //zone Déplacement
        serpent = new SerpentV2();
        fond.add(serpent);
 
        this.addKeyListener(this);
        this.setSize(500,500);
        this.setContentPane(fond);
        this.setVisible(true);
    }
    public void keyPressed(KeyEvent e){
        int nombreToucheJ1 = e.getKeyCode();
        int nombreToucheJ2 = e.getKeyCode();
        if(nombreToucheJ1 ==KeyEvent.VK_UP){
            serpent.hautJ1();
        }
        if(nombreToucheJ1==KeyEvent.VK_DOWN){
            serpent.basJ1();
        }
        if(nombreToucheJ1==KeyEvent.VK_LEFT){
            serpent.gaucheJ1();
        }
        if(nombreToucheJ1==KeyEvent.VK_RIGHT){
            serpent.droiteJ1();
        }
        //touche joueur 2
        if(nombreToucheJ2 ==KeyEvent.VK_Z){
            serpent.hautJ2();
        }
        if(nombreToucheJ2==KeyEvent.VK_S){
            serpent.basJ2();
        }
        if(nombreToucheJ2==KeyEvent.VK_Q){
            serpent.gaucheJ2();
        }
        if(nombreToucheJ2==KeyEvent.VK_D){
            serpent.droiteJ2();
        }
    }  
    public void keyReleased(KeyEvent e){
 
    }
    public void keyTyped(KeyEvent e){
 
    }
}

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
import java.awt.*;
import javax.swing.*;
public class SerpentV2 extends JPanel{
    private int xJ1 , yJ1 , xJ2, yJ2;
    private int taille;
    public SerpentV2(){
        super();
        this.setBackground(Color.BLUE);
        taille = 20;
        yJ1 = 25;
        xJ1 = 50;
 
        yJ2 = 25;
        xJ2 = 400;
 
        this.setPreferredSize(new Dimension(500,300));
    }
 
    public void gaucheJ1(){
        xJ1 = xJ1-5;
        taille = taille;
        repaint();
    }
 
    public void droiteJ1(){
        xJ1 = xJ1+5;
        taille = taille;
        repaint();
    }
 
    public void basJ1(){
        yJ1 = yJ1+5;
        taille = taille;
        repaint();
    }
 
    public void hautJ1(){
        yJ1 = yJ1-5;
        taille = taille;
        repaint();
    }
    // pour le joueur 2
     public void gaucheJ2(){
        xJ2 = xJ2-5;
        taille = taille;
        repaint();
    }
 
    public void droiteJ2(){
        xJ2 = xJ2+5;
        taille = taille;
        repaint();
    }
 
    public void basJ2(){
        yJ2 = yJ2+5;
        taille = taille;
        repaint();
    }
 
    public void hautJ2(){
        yJ2 = yJ2-5;
        taille = taille;
        repaint();
    }
    public void paintComponent(Graphics g){
        //fond derriere carre Rose
        g.setColor(Color.BLUE);
        g.fillRect(xJ2-5,yJ2-5,taille+10,taille+10);
 
        //fond derriere carre Jaune
        g.setColor(Color.BLUE);
        g.fillRect(xJ1-5,yJ1-5,taille+10,taille+10);
 
        for( int i = 0 ; (i*5)<500;i++){
            if(i%2 == 0){
                 //carre blanc
                g.setColor(Color.BLACK);
                g.fillRect(i*5,50,5,5);
            }
            if(i%2 == 1){
                //carre noir
                g.setColor(Color.WHITE);
                g.fillRect(i*5,50,5,5);
            }
        }
        for( int i = 0 ; (i*5)<500;i++){
            if(i%2 == 1){
                 //carre blanc
                g.setColor(Color.BLACK);
                g.fillRect(i*5,55,5,5);
            }
            if(i%2 == 0){
                //carre noir
                g.setColor(Color.WHITE);
                g.fillRect(i*5,55,5,5);
            }
        }
 
 
        //carre jaune
        g.setColor(Color.YELLOW);
        g.fillRect(xJ1,yJ1,taille,taille);
 
        //carre rose
        g.setColor(Color.PINK);
        g.fillRect(xJ2,yJ2,taille,taille);
 
        //ligne de depart
 
    }
}