Bonjour,
J'ai un souci pour transférer une variable d'une classe à une autre, c'est la variable reponseJ1, si quelqu'un pouvais m'aider.
//Classe MainA:
-------------------------------------------------
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 public class MainA { public static void main(String[] args) { KeyEventTestA bJ1 = new KeyEventTestA(); int rep = 12; if(bJ1.getReponseJ1()==1) { rep = 1;} else if(bJ1.getReponseJ1()==2) { rep = 2;} else if(bJ1.getReponseJ1()==3) { rep = 3;} ThreadTestB tB = new ThreadTestB("Blabla"); tB.start(); ThreadTestC tC = new ThreadTestC(rep); tC.start(); } }
//Classe KeyEventTestA:
-------------------------------------------------
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 import java.awt.event.KeyEvent; import java.awt.event.KeyListener; class KeyEventTestA implements KeyListener{ private boolean touchea = false; private boolean toucheq = false; private int reponseJ1; public int getReponseJ1() { return reponseJ1; } public void setReponseJ1(int reponseJ1) { this.reponseJ1 = reponseJ1; } public boolean getTouchea() { return touchea; } public void setTouchea(boolean touchea) { this.touchea = touchea; } public void keyTyped(KeyEvent e) { if(touchea == true) { reponseJ1 = 1; } else if(toucheq == true) { reponseJ1 = 2; } else {reponseJ1 = 3;} try { Thread.sleep(200); } catch (InterruptedException ie) { ie.printStackTrace(); } } public void keyPressed(KeyEvent e) { if (e.getKeyChar()=='a') { touchea = true; } else if (e.getKeyChar() == 'q') { toucheq = true; } } public void keyReleased(KeyEvent e) { if (e.getKeyChar()=='a') { touchea = false; } else if (e.getKeyChar() == 'q') { toucheq = false; } } }
//Classe ThreadTestB:
-------------------------------------------------
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 class ThreadTestB extends Thread { private String name; public ThreadTestB(String name) { this.name = name; } public void run() { for(int i=0;i<10;i++) { System.out.println(name); try { Thread.sleep(500); } catch (InterruptedException e) {} } } }
//Classe ThreadTestC:
Merci pour l'aide que vous pourriez m'apporter.
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 class ThreadTestC extends Thread { private int name; public ThreadTestC(int name) { this.name = name; } public void run() { for(int i=0;i<10;i++) { System.out.println(name); try { Thread.sleep(500); } catch (InterruptedException e) {} } } }
Partager