Ayant creer 2 fenêtres en IHM, Je veut veut recupérer la variable 'text' d'un TextField dans la 1ère fenêtre et l'afficher dans un Label de la 2ème
fenètre!
Le problème est que la première saisie dans 'text' n'affiche pas immédiatement son contenu dans le Label dans la 2ème fenêtre; il l'affiche avec un décalage....
du genre:
saisie N°1("Paul") Affiche Null
saisie N°2("Pière") Affiche Paul
saisie N°3("Jean") Affiche Pièrre
...
or ce que je veux c'est:
saisie N°1("Paul") Affiche Paul
saisie N°2("Pière") Affiche Pièrre
saisie N°3("Jean") Affiche Jean
...
voici mon code
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 //fenètre 1 public class Prog1 extends JFrame implements ActionListener { public JTextField text; public JLabel label; public JPanel A; public JButton bok; public Prog1() { super("Page 1"); A = new JPanel(); A.add(label = new JLabel("Quel eest votre nom ? ")); A.add(text = new JTextField(20)); A.add(bok = new JButton("OK")); bok.addActionListener(this); this.add(A); this.pack(); } public void actionPerformed(ActionEvent e) { Prog2 prg2 = new Prog2(); if(e.getSource() == bok) { prg2.utile = text.getText().toString(); this.setVisible(false); prg2.show(); } } public static void main(String[] args) { Prog1 prg = new Prog1(); prg.setVisible(true); } }comment puis-je corriger se probleme ?
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 //fenètre 2 public class Prog2 extends JFrame implements ActionListener { public JLabel label; public JPanel B; public JButton retour; public static String utile; public Prog2() { super("Page 2"); B = new JPanel(); B.add(label = new JLabel("Bonjour "+utile+" !")); B.add(retour = new JButton("Retour")); retour.addActionListener(this); this.add(B); this.pack(); } public void actionPerformed(ActionEvent e) { Prog1 prg = new Prog1(); if(e.getSource() == retour) { this.setVisible(false); prg.show(); } } }
Partager