Pouvez vous ce qui fait que je n'ai pas le résultat voulu à partir de ce code. A savoir que j'ai mis la fonction main dans une classe à part. J'ai placé un bouton "Ajouter" sur le pan2 qui doit s'afficher quand on clique sur le menu2.
Merci
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.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JCheckBoxMenuItem; import java.awt.Color; import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JPanel; import javax.swing.JButton; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JRadioButtonMenuItem; public class Fenetre extends JFrame implements ActionListener { //CREATION DU MENU private JMenuBar menuBar = new JMenuBar(); private JMenu menu1 = new JMenu("Facturation"); private JMenu menu2 = new JMenu("Articles"); private JMenu menu3 = new JMenu("Journal"); private JMenu menu4 = new JMenu("Perso"); // Sous menu private JMenuItem item1 = new JMenuItem("Définitive"); private JMenuItem item12 = new JMenuItem("Proforma"); JPanel pan1 = new JPanel(); // instanciation de notre objet jpanel JPanel pan2 = new JPanel(); JPanel pan3 = new JPanel(); JPanel pan4 = new JPanel(); private JButton bouton_ajout = new JButton("+ Ajouter"); public Fenetre(){ this.setTitle("anKara"); this.setSize(400, 500); this.setLocationRelativeTo(null); //pan.setBackground(Color.GRAY); this.setContentPane(pan1); //On prévient notre JFrame que notre JPanel pan1 sera son content pane this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Attribution des items au menu correspondant this.menu1.add(item1); this.menu1.add(item12); //Définition du menu this.menuBar.add(menu1); this.menuBar.add(menu2); this.menuBar.add(menu3); this.menuBar.add(menu4); this.setJMenuBar(menuBar); menu1.addActionListener(this); menu2.addActionListener(this); //positionnement du bouton Ajouter pan2.setLayout(null); pan2.add(bouton_ajout); bouton_ajout.setBounds(2,10,100,40); getContentPane().add(pan2); this.setVisible(true); } // Fonction swap pour changer l'affichage selon le menu public void swap(JPanel newContainer) { JPanel content = (JPanel) getContentPane(); content.removeAll(); content.add(newContainer); content.revalidate(); content.repaint(); } public void actionPerformed(ActionEvent e) { if (e.getSource() == this.menu1) { swap(this.pan1); } else if (e.getSource() == this.menu2) { swap(this.pan2); } else if (e.getSource() == this.menu3) { swap(this.pan3); } else if (e.getSource() == this.menu4) { swap(this.pan4); } } }
Partager