Bonjour.
J'ai developpé une application qui ouvre une applet et à l'ouverture, une boite de dialogue s'ouvre pour demander quel fichier texte doit etre lu. Pour cela, j'ai créer une boite de dialogue avec trois boutons : 1 pour le fichier texte 1, 1 pour le fichier texte 2 et 1 pour annuler.
J'ai d'abord créé ma boite de dialogue avec Dialog et awt et ca marche bien.
Voici le code :
J'ai voulu passer en swing mes boutons et mes labels pour que ce soit plus joli et la, je n'ai plus d'action sur les boutons. J'ai fait différentes recherches et j'ai trouvés des codes mais maintenant, je passe bien dans le "clic bouton" mais la boite de dialogue ne se ferme jamais et je ne peux jamais revenir sur mon applet principale.
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
90 import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.io.RandomAccessFile; import java.io.*; import javax.swing.*; public class boiteinit extends Dialog { private interfaceapplet app; private Button bnouveau=null; private Button bancien=null; private Button bannule=null; private Label lmessage=null; private Panel p=null; public boiteinit (interfaceapplet app,Frame frame, String titre) { // Appel du constructeur de la classe Dialog en mode modal super (frame, titre, true); this.app=app; } public void initialisation() { p =new Panel(null); bnouveau=new Button("B1"); bancien=new Button("B2"); bannule=new Button("Annuler"); lmessage=new Label("Choix du fichier à ouvrir"); p.setSize(600,600); setLayout(null); lmessage.setBounds(200,0,190,20); bnouveau.setBounds(40,30,200,20); bancien.setBounds(250,30,200,20); bannule.setBounds(160,60,200,20); p.add(bnouveau); p.add(bancien); p.add(lmessage); p.add(bannule); } public void showBoiteinit (Component parent, String titre) { // Recherche du parent qui est un Frame Component frame = parent; while ( frame != null && !(frame instanceof Frame)) frame = frame.getParent (); // Création d'une instance de MessageBox setBackground(Color.gray); boiteinit boite = new boiteinit (app,(Frame)frame, titre); boite.setSize(520,130); boite.setLocationRelativeTo(null); initialisation(); boite.add(p); boite.setVisible(true); } public boolean action (Event event, Object eventArg) { if(event.arg=="B1") { app.monFichier="b1.txt"; } if(event.arg=="B2") { app.monFichier="b2.txt"; } if(event.arg=="Annuler") { System.exit(0); } // Destruction de la boite de message dispose (); return true; } }
Voici mon code :
Mon String app.MonFichier se rempli bien mais je ne retrourne pas sur mon applet pour le charger.
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
90
91
92
93
94
95
96
97
98
99 import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.io.RandomAccessFile; import java.io.*; import javax.swing.*; public class boiteinit extends JDialog { private interfaceapplet app; private JButton bnouveau=null; private JButton bancien=null; private JButton bannule=null; private JLabel lmessage=null; private JPanel p=null; public boiteinit (interfaceapplet app,JFrame frame, String titre) { super (frame, titre,true); this.app=app; p =new JPanel(null); bnouveau=new JButton("B1"); bancien=new JButton("B2"); bannule=new JButton("Annuler"); lmessage=new JLabel("Choix du fichier à ouvrir"); p.setSize(600,600); setLayout(null); lmessage.setBounds(200,0,190,20); bnouveau.setBounds(40,30,200,20); bancien.setBounds(250,30,200,20); bannule.setBounds(160,60,200,20); this.bnouveau.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { nouveaufichier(); boiteinit.this.dispose(); } }); this.bancien.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ancienfichier(); boiteinit.this.dispose(); } }); this.bannule.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); boiteinit.this.dispose(); } }); p.add(bnouveau); p.add(bancien); p.add(lmessage); p.add(bannule); } public void nouveaufichier() { app.monFichier="b1.txt"; } public void ancienfichier() { app.monFichier="b2.txt"; } public void initialisation() { ... } public void showBoiteinit (Component parent, String titre) { // Recherche du parent qui est un Frame Component frame = parent; while ( frame != null && !(frame instanceof JFrame)) frame = frame.getParent (); // Création d'une instance de MessageBox setBackground(Color.gray); boiteinit boite = new boiteinit (app,(JFrame)frame, titre); boite.setSize(520,130); boite.setLocationRelativeTo(null); initialisation(); boite.add(p); boite.setVisible(true); }
Partager