Bonsoir,

Quelqu'un peut il me dire si on peut créer une boucle dans un listener de button...peut on appeller l'action d'un autre button dans un button?
Bref, je ne suis pas expert mais je cherche à comprendre pour résoudre mon problème. J'ai tenté des boucles dans createQuestion() mais a priori ce n'est pas cela...après dans le button next....je suis perdu. Si vous pouvez me lancer des pistes de travail svp...Je vous en remercie d'avance.



Le code est le suivant:
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
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
 
package performyourgame.quizz.prepabac;
 
/*
 * Author: Romuald Grandin
 * Date: 07/09/2015
 *
 *
 * Programme: L'objectif de cette application est de répondre à différentes questions sous forme de quiz dans un temps imparti.
 */
 
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
 
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
 
public class Main {
	// ====================DECLARATION ET
	// INITIALISATION===========================//
 
	static private JTextField question = new JTextField(40);
	static private JTextField reponse = new JTextField(40);
	static private int index, score, total;
	static private JButton start = new JButton("START");
	static private JButton next = new JButton("NEXT");
	static private JButton validate = new JButton("VALIDER");
	static private JButton stop = new JButton("STOP");
	static private JButton closeFrame = new JButton("QUITTER");
	static private ArrayList<Integer> indexesAlreadyTaken = new ArrayList<>();
	static private String preguntas, reponseAnswer, reponseClavier;
	static private long endTime, startTime;
	static String[][] Objects = getData();
 
	public static void main(String[] args) {
		// =====================MISE EN FORME DE LA
		// FENETRE==========================//
		final JFrame frame = new JFrame();
		frame.setVisible(true);
		frame.setTitle("QUIZ PREPA BAC");
		frame.setSize(800, 600);
		frame.setResizable(true);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// =================================================================//
		// ======================FOND ECRAN============================//
		JPanel content = new ImagePanel(new ImageIcon("design.jpeg").getImage());
		// =================================================================//
		// ======================AJOUT PANNEAU DANS LA
		// FENETRE==============================//
		frame.setContentPane(content);
		// =================================================================//
		// ======================CHOIX TAILLE ET POLICE DU
		// JLABEL==============================//
		Font f = new Font("Serif", Font.PLAIN, 36); // par exemple
		question.setFont(f);
		reponse.setFont(f);
		Font ft = new Font("Serif", Font.PLAIN, 40);
		validate.setFont(ft);
		closeFrame.setFont(ft);
		stop.setFont(ft);
		start.setFont(ft);
		next.setFont(ft);
		// =================================================================//
		// ============MISE EN FORME DES BOUTONS MISE A ZERO ET
		// QUITTER============================//
		question.setBackground(Color.white);
		question.setBorder(null);
		reponse.setBackground(Color.white);
		reponse.setBorder(null);
		next.setEnabled(false);
		// =======================================================================================//
		// ======================AJOUT DES ELEMENTS AU
		// PANNEAU============================//
		content.add(question);
		content.add(reponse);
		content.add(start);
		content.add(next);
		content.add(validate);
		content.add(stop);
		content.add(closeFrame);
		// =================================================================//
		// =====================AFFICHAGE ICONE COIN DE
		// FENETRE=======================//
		frame.setIconImage(new ImageIcon("fGris.jpeg").getImage());
		// =================================================================//
		frame.validate();
		// =================================================================//
		// ==========EVENEMENT EN CAS DE CLIC SUR BOUTON
		// START============================//
		start.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == start) {
					startTime = System.currentTimeMillis();
 
					reponse.setText("");
					createQuestion();
				}
			}
		});
		// ===============================================================================//
		// ==========EVENEMENT EN CAS DE CLIC SUR BOUTO NEXT
		// ============================//
		next.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == next) {
					validate.setEnabled(true);
					reponse.setText("");
					if(total == Objects.length ){
						stop.getActionCommand();
					} else{
						createQuestion();
						next.setEnabled(false);	
					}
 
				}
			}
		});
		// ===============================================================================//
		// ===============================================================================//
		// ==========EVENEMENT EN CAS DE CLIC SUR BOUTON
		// STOP============================//
		stop.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == stop) {
 
					endTime = System.currentTimeMillis();
 
					question.setText(String.format(
							"Tu as terminé !! Ton score est de << %d/%d >>.",
							score, total));
					reponse.setText(String
							.format("Il vous a fallu environ %d secondes pour répondre aux %d questions.",
									getElapsedTimeInSeconds(), total));
 
				}
			}
 
		});
		// ===============================================================================//
		// ==========EVENEMENT EN CAS DE CLIC SUR BOUTON
		// QUITTER============================//
		closeFrame.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == closeFrame) {
					frame.dispose();
				}
			}
		});
		// ===============================================================================//
		// ==================EVENEMENT EN CAS DE CLIC SUR BOUTON
		// VALIDER==============//
		validate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == validate) {
					start.setEnabled(false);
					next.setEnabled(false);
 
					question.setText("Cliquez sur NEXT pour continuer ou STOP si vous vous arrêtez!!");
					reponseClavier = reponse.getText();
 
					if (reponseClavier.equalsIgnoreCase(reponseAnswer)) {
						reponse.setText("Bonne réponse!! Tu es un champion! ");
						score++;
						total++;
					} else {
						reponse.setText(String.format(
								"Dommage ! La bonne réponse était: %s. ",
								reponseAnswer));
						total++;
					}
					next.setEnabled(true);
					validate.setEnabled(false);
				}
			}
		});
	}
 
	// ============================================================================
	// //
	protected static int getElapsedTimeInSeconds() {
		long timeElapsed = (endTime - startTime);
		return (int) (timeElapsed / 1000);
	}
 
	// ==================================================================== //
	static String[][] getData() {
 
		String[][] data = { { "Qui mord?", "Crocodile" },
				{ "Qui fait cui cui?", "Canari" },
				{ "Qui est Willy?", "Baleine" },
				{ "Quel est ce fruit?", "Orange" } };
		return data;
	}
 
	// =================================================================//
 
	// ====================== FONCTION TIMER ========================== //
	// =======================FONCTION CREATION QUESTION AU
	// HASARD========================//
	public static void createQuestion() {
 
		start.setEnabled(false);
		reponse.setText("");
 
			do {
				Random random = new Random();
				index = random.nextInt(Objects.length);
 
				preguntas = Objects[index][0];
				reponseAnswer = Objects[index][1];
			} while (indexesAlreadyTaken.contains(index));
 
			indexesAlreadyTaken.add(index);
 
			question.setText(String.format("%s", preguntas));
		};
 
 
	// ===============================================================================//
}
 
/*
 * Classe image background
 */
class ImagePanel extends JPanel {
 
	private static final long serialVersionUID = 1L;
	private Image img;
 
	public ImagePanel(String img) {
		this(new ImageIcon(img).getImage());
	}
 
	public ImagePanel(Image img) {
		this.img = img;
	}
 
	public void paintComponent(Graphics g) {
		g.drawImage(img, 0, 0, null);
	}
	/*
	 * ====================================================
	 */
}