Bonsoir,

J'avance pas à pas dans un projet...mais là je bloque pour comprendre comment on met en place un keylistener avec la touche entrée sur un bouton.

J'aimerai à chaque clic sur entrée, le temps du bouton 1 s'arrête puis le 2, 3, etc...Mais je n'arrive déjà pas à l'arrêter sur un bouton. Quelqu'un peut-il m'expliquer ou m'indiquer mes erreurs, s'il vous plaît?

Je vous remercie par avance.

classe MainActivity
Code java : 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
package performyourgame.chrono.demifond;
 
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
 
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
 
public class MainActivity extends JFrame implements KeyListener {
 
	static Chrono chrono = new Chrono();
 
	private static JButton btnStart = new JButton("START");
	private static JButton[] btnLap = new JButton[13];
 
	// private static JButton btnStop = new JButton("STOP");
 
	private static JTextField[] time = new JTextField[13];
 
	// private static JButton btnPause = new JButton("PAUSE");
	// private static JButton btnResume = new JButton("REPRENDRE");
	private static JButton btnErase = new JButton("REMISE A ZERO");
	private static JButton btnQuit = new JButton("QUITTER");
 
	private static JLabel allRight = new JLabel();
 
	public static void main(String[] args) {
 
		JFrame frame = new JFrame();
		frame.setTitle("3 fois 500 mètres");
		frame.setSize(900, 600);
		frame.setResizable(true);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
		frame.setIconImage(new ImageIcon("Les-7-merveilles-du-monde.jpg").getImage());
 
		frame.setVisible(true);
		JPanel content = new JPanel();
		frame.setContentPane(content);
 
		content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
 
		Font f = new Font(" TimesRoman ", Font.BOLD + Font.ITALIC, 30);
		btnStart.setFont(f);
		btnErase.setFont(f);
		btnQuit.setFont(f);
 
		content.add(btnStart);
 
		// content.add(btnStop);
		// content.add(btnPause);
		// content.add(btnResume);
 
		for (int i = 1; i < 13; i++) {
			JButton jb = new JButton();
			JTextField jt = new JTextField();
			btnLap[i] = jb;
			time[i] = jt;
			btnLap[i].setText("Temps" + i);
			content.add(jb);
			content.add(jt);
			;
		}
 
		content.add(btnErase);
		content.add(btnQuit);
 
		frame.pack();
 
		allRight.setText("@2015, All's right reserved Romuald Grandin");
		content.add(allRight);
 
		frame.validate();
 
		// ================================================================= //
 
		// ====================================================================
		// //
 
		// Add action listener to button
		btnStart.addActionListener(new ActionListener() {
 
			public void actionPerformed(ActionEvent e) {
				// Execute when button is pressed
				chrono.start();
 
				for (int i = 1; i < 13; i++) {
					final int j = i;
					time[j].setText(chrono.getDureeTxt());
				}
 
			}
		});
 
		// Add action listener to button
 
		for (int i = 1; i < 13; i++) {
			final int j = i;
 
			btnLap[j].addActionListener(new ActionListener() {
 
				public void actionPerformed(ActionEvent e) {
					// Execute when button is pressed
					chrono.timeLap();
					time[j].setText(chrono.getDureeTxt());
				}
			});
		}
 
		// Add action listener to button
		btnErase.addActionListener(new ActionListener() {
 
			public void actionPerformed(ActionEvent e) {
				// Execute when button is pressed
				for (int i = 1; i < 13; i++) {
					final int j = i;
					time[j].setText("");
				}
			}
		});
 
		// Add action listener to button
		btnQuit.addActionListener(new ActionListener() {
 
			public void actionPerformed(ActionEvent e) {
				// Execute when button is pressed
				frame.dispose();
			}
		});
 
		// System.out.println(chrono.getDureeMs()); // affichage du résultat en
		// millisecondes
		// System.out.println(chrono.getDureeSec()); // affichage en secondes
		// System.out.println(chrono.getDureeTxt()); // affichage au format "1 h
		// 26 min 32 s"
 
	}
 
	@Override
	public void keyTyped(KeyEvent e) {
 
	}
 
	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getSource() == btnLap) {
			if (e.getKeyCode() == KeyEvent.VK_ENTER) {
 
			}
		}
 
	}
 
	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
 
	}
 
}

classe chrono

Code java : 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
package performyourgame.chrono.demifond;
 
 
public class Chrono {
 
    private long tempsDepart=0;
    private long tempsFin=0;
    private long pauseDepart=0;
    private long pauseFin=0;
    private long duree=0;
 
    private long tempsLap = 0;
 
    public void start()
        {
        tempsDepart=System.currentTimeMillis();
        tempsFin=0;
        pauseDepart=0;
        pauseFin=0;
        duree=0;
        }
 
    public void pause()
        {
        if(tempsDepart==0) {return;}
        pauseDepart=System.currentTimeMillis();
        }
 
    public void timeLap()
    {
    	tempsLap = System.currentTimeMillis();
    	duree=(tempsLap-tempsDepart);
    }
 
    public void resume()
        {
        if(tempsDepart==0) {return;}
        if(pauseDepart==0) {return;}
        pauseFin=System.currentTimeMillis();
        tempsDepart=tempsDepart+pauseFin-pauseDepart;
        tempsFin=0;
        pauseDepart=0;
        pauseFin=0;
        duree=0;
        }
 
    public void stop()
        {
        if(tempsDepart==0) {return;}
        tempsFin=System.currentTimeMillis();
        duree=(tempsFin-tempsDepart) - (pauseFin-pauseDepart);
        tempsDepart=0;
        tempsFin=0;
        pauseDepart=0;
        pauseFin=0;
        }        
 
    public long getDureeSec()
        {
        return duree/1000;
        }
 
    public long getDureeMs()
        {
        return duree;
        }        
 
    public String getDureeTxt()
        {
        return timeToHMS(getDureeSec());
        }
 
    public static String timeToHMS(long tempsS) {
 
        // IN : (long) temps en secondes
        // OUT : (String) temps au format texte : "1 h 26 min 3 s"
 
        int h = (int) (tempsS / 3600);
        int m = (int) ((tempsS % 3600) / 60);
        int s = (int) (tempsS % 60);
 
        String r="";
 
        if(h>0) {r+=h+" h ";}
        if(m>0) {r+=m+" min ";}
        if(s>0) {r+=s+" s";}
        if(h<=0 && m<=0 && s<=0) {r=h +" h" + m + " m" + s + " s";}
 
        return r;
        }
 
    }

Mille Merci pour votre aide.