Bonjour, les gars !
Quelqu'un pourrait-il m'aider? Je suis débutant en Java et je ne sais pas encore mettre de l'ordre dans mes codes.
Je vous donne mon code. Quand je clique sur btnReady, huit lettres sont générées aléatoirement, mais je n'arrive pas à les affecter aux huit boutons.
En plus, je demande du code pour que lorsque je clique sur l'un des huit boutons, que la lettre inscrite sur le bouton soit affectée dans un JTextField de la grille.
Besoin d'aide en urgence pour ce travail de classe. Merci
Voici mon code
=-=DEBUT DU CODE=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JPanel;
public class LettresBoutons extends JPanel {
private GridBagLayout layout;
private GridBagConstraints contraintes;
private JButton btnReady;
private ButtonHandler btnHandler;
private final String CONSONNES = "bcdfghjklmnpqrstvwxz";
private final String VOYELLES = "aeiouy";
private final String AUTRES = "bacedifoguhyjakelimonupyqaresitovuwyxaz";
public LettresBoutons() {
layout = new GridBagLayout();
setLayout(layout);
contraintes = new GridBagConstraints();
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 4));
panel.setLayout(new GridLayout(2, 4));
panel.add(new JButton(" "));
panel.add(new JButton(" "));
panel.add(new JButton(" "));
panel.add(new JButton(" "));
panel.add(new JButton(" "));
panel.add(new JButton(" "));
panel.add(new JButton(" "));
panel.add(new JButton(" "));
btnReady = new JButton("Ready ?");
btnHandler = new ButtonHandler();
btnReady.addActionListener((ActionListener) btnHandler);
addComp(panel, 0, 0, 1, 1);
addComp(btnReady, 1, 0, 4, 1);
}
private void JetLettres() {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 4));
final int nbL = 8;
final int nbVoyelles = 3;
final int nbConsonnes = 3;
final int nbAutres = 2;
ArrayList<String> lettres = new ArrayList<String>();
Random random = new Random(System.currentTimeMillis());
for (int i = 0; i < nbVoyelles; ++i) {
char voyelle = VOYELLES.charAt(random.nextInt(VOYELLES.length()));
lettres.add("" + voyelle);
}
for (int j = 0; j < nbConsonnes; ++j) {
char consonnes = CONSONNES.charAt(random.nextInt(CONSONNES.length()));
lettres.add("" + consonnes);
}
for (int k = 0; k < nbAutres; ++k) {
char autre = AUTRES.charAt(random.nextInt(AUTRES.length()));
lettres.add("" + autre);
}
Collections.shuffle(lettres);
System.out.println(lettres);
for (int m = 0; m < 8; ++m) {
panel.add(new JButton(lettres.get(m)));
// ============================================================
// TOUT SE PASSE COMME JE VEUX JUSQU'A CE NIVEAU OU JE VOUDRAIS
// AFFECTER UNE LETTRE A CHAQUE BOUTON.
// MAIS JE NE M'EN SORS PAS.
// ============================================================
}
}
private void addComp(Component comp, int x, int y, int h, int w) {
contraintes.gridx = y;
contraintes.gridy = x;
contraintes.gridwidth = h;
contraintes.gridheight = w;
layout.setConstraints(comp, contraintes);
this.add(comp);
}
private class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (event.getSource() == btnReady) {
JetLettres();
}
}
}
}
=-=FIN DU CODE=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=