Bonjour, j'ai un soucis je ne sais pas du tout comment faire pour que ma ComboBox ( qui permets de choisir le nombre de joueur) puissent intéragir avec ma fenêtre qui demande le prénom des joueurs. J'aimerais savoir comment faire Merci d'avance. (Je sais pas si je suis assez clair...) N'hésitez pas a me demandez
Code de la ComboBox
et le code de la fenetre qui demande le nom des joueurs :
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 import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.text.NumberFormat; public class FenetreCombo extends JFrame { private static final long serialVersionUID = 1L; public JPanel container2 = new JPanel(); public JLabel label2 = new JLabel("Séléctionnez le nombre de joueur "); public JFormattedTextField txtfield = new JFormattedTextField(NumberFormat.getIntegerInstance()); public JButton b2 = new JButton ("OK"); JComboBox combo; public FenetreCombo(){ String choixcombo[]= {"2","3","4","5"}; this.setTitle("AppliSoirée"); this.setSize(500, 500); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); JPanel top2 = new JPanel(); Font police = new Font("Arial", Font.BOLD, 14);//Selection de police d'écriture + taille txtfield.setFont(police); txtfield.setPreferredSize(new Dimension(50, 30)); //tAILLE DE jtEXTFILD txtfield .setForeground(Color.RED); b2.addActionListener(combo); combo = new JComboBox (choixcombo); top2.add(label2); top2.add(combo); top2.add(txtfield); //Ajoute le TextField top2.add(b2); //Ajoute le bouton OK. container2.add(top2, BorderLayout.NORTH); this.setContentPane(container2); this.setVisible(true); container2.setBackground(Color.CYAN);//Couleur de fond b2.setBackground(Color.green); container2.setLayout(new BorderLayout()); combo.addItemListener(new ItemListener(){ public void itemStateChanged(ItemEvent ie){ String str = (String)combo.getSelectedItem(); txtfield.setText(str); } }); b2.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { // Fenetre1.setVisible(false); dispose(); Fenetre1 fen1 =new Fenetre1(); //Lorsque on clique sur ok ouvre Fenetre1 } }); }
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 //Les imports habituels import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.text.NumberFormat; import java.util.Scanner; import javax.swing.JButton; import javax.swing.JFormattedTextField; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; public class Fenetre1 extends JFrame { /** * */ Scanner sc = new Scanner(System.in); private static final long serialVersionUID = 1L; private JPanel container1 = new JPanel(); private JLabel label1 = new JLabel("Entrez nom Joueur 1"); private JFormattedTextField jtf1 = new JFormattedTextField(NumberFormat.getIntegerInstance()); private JButton b1 = new JButton ("OK"); public Fenetre1(){ String nom1; String nom2; String nom3; String nom4; this.setTitle("AppliSoirée"); this.setSize(500, 500); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); JPanel top1 = new JPanel(); Font police = new Font("Arial", Font.BOLD, 14);//Selection de police d'écriture + taille jtf1.setFont(police); jtf1.setPreferredSize(new Dimension(150, 30)); //tAILLE DE jtEXTFILD jtf1.setForeground(Color.RED); b1.addActionListener(null); top1.add(label1); top1.add(jtf1); //Ajoute le TextField top1.add(b1); //Ajoute le bouton OK. container1.add(top1, BorderLayout.NORTH); this.setContentPane(container1); this.setVisible(true); container1.setBackground(Color.CYAN);//Couleur de fond b1.setBackground(Color.green); container1.setLayout(new BorderLayout()); //int i = jtf.getText(); // i = Integer.parseInt(jtf.getText()); // if( i == 1){System.out.println("joeur tu bois");} // final FenetreCombo combo = new FenetreCombo(); // this.add(FenetreCombo.getSelectedItem()); // if(combo.getSelectedItem() =4){ } b1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { dispose(); // Fenetre1.setVisible(false); Fenetre fen=new Fenetre(); } }); } ;}
Partager