Bonjour,
J'ai une JFrame, dans laquelle je mets un JPanel auquel je donne une image de fond (PanelJardin). Jusque là tout marche. J'ai aussi ajouté une barre de menus en haut.
A ce JPanel, qui prend toute la place dans la fenêtre, j'ai ajouté un JLabel qui contient une image, serré à gauche (BorderLayout.WEST). Ca aussi ca marche.
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 public class Atoll extends JFrame { private JLabel iconLabel; private PanelJardin generalPanel; private JMenuBar menuBar = new JMenuBar(); private JMenu fonctions = new JMenu("Fonctions"); private JMenuItem lecture = new JMenuItem ("Lecture de résultats d'analyse"); private JMenuItem etud_granulo = new JMenuItem("Etude granulométrique"); private JMenuItem diagrammes = new JMenuItem("Diagrammes"); private JMenu options = new JMenu("Options"); private JMenu adresses = new JMenu("Modifier"); private JMenuItem adresse_labo = new JMenuItem("Coordonnées du laboratoire"); private JMenuItem path_jrxml = new JMenuItem("Chemin d'accès au fichier de génération de rapport"); private JMenuItem fiches = new JMenuItem("Gérer les règles d'interprétation physique"); // private JMenuItem formules = new JMenuItem("Consulter les formules"); private JMenu programme = new JMenu("Programme"); private JMenuItem a_propos = new JMenuItem("A propos d'Atoll"); private JMenuItem disconnect = new JMenuItem("Se déconnecter"); private JMenuItem retour_accueil = new JMenuItem("Retour à l'accueil"); private JMenuItem quitter = new JMenuItem("Quitter Atoll"); public Atoll(JFrame parent, String title, boolean modal){ super(); this.setTitle(title); this.setSize(800, 600); this.setLocationRelativeTo(null); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.initComponent(); } private void initComponent(){ UIManager.getDefaults().put("Panel.background", new Color(0, 0, 0, 0)); // background jpanel par defaut = transparent // //Menu this.fonctions.add(lecture); this.fonctions.add(etud_granulo); this.fonctions.add(diagrammes); this.menuBar.add(fonctions); this.options.add(adresses); this.adresses.add(adresse_labo); this.adresses.add(path_jrxml); this.options.add(fiches); this.menuBar.add(options); this.programme.add(retour_accueil); this.programme.add(a_propos); this.programme.add(disconnect); this.programme.add(quitter); this.menuBar.add(programme); this.setJMenuBar(menuBar); this.a_propos.addActionListener(new aProposListener()); this.quitter.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent arg0) { System.exit(0);}}); this.retour_accueil.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent arg0) { getContentPane().removeAll(); initComponent(); validate();}}); this.disconnect.addActionListener(new disconnectListener()); this.path_jrxml.addActionListener(new PathListener()); this.fiches.addActionListener(new fichesListener()); this.analyse.addActionListener(new AnalyseListener()); Image img = new ImageIcon(TheMain.fichier[0]+"jardin1024.jpg").getImage(); generalPanel = new PanelJardin (img); ImageIcon imgicn = new ImageIcon (TheMain.fichier[0]+"Logo copie.png"); // path de l'image + nom imgicn = new ImageIcon(resize(imgicn.getImage(),262,550)); // meme fonction que celle de la faq, redimensionnement d'une image. iconLabel = new JLabel(imgicn); iconLabel.setBackground(new Color(0, 0, 0, 255)); Image icone = new ImageIcon(TheMain.fichier[0]+"icone.gif").getImage(); this.setIconImage(icone); this.getContentPane().add(generalPanel); generalPanel.setLayout(new BorderLayout()); generalPanel.add(iconLabel, BorderLayout.WEST); }rendu : ici
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 public class PanelJardin extends JPanel{ private Image img; PanelJardin (Image image){ this.img = image; } PanelJardin(){ this.img = new ImageIcon (TheMain.fichier[0]+"jardin1024.jpg").getImage(); } /** Surcharge de la fonction paintComponent() pour afficher notre image */ public void paintComponent(Graphics g) { super.paintComponents(g); g.drawImage(img,0,0,null); } }
Bon, mon problème, c'est que quand je veux ajouter un bouton à droite, il prend toute la place. Du coup, si je veux l'ajouter dans un JPanel, celui-ci vient mettre le dawa : rendu
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 public class Atoll extends JFrame { private JLabel iconLabel; private PanelJardin generalPanel; private JButton analyse = new JButton("Interprétation des résultats"); private JMenuBar menuBar = new JMenuBar(); private JMenu fonctions = new JMenu("Fonctions"); private JMenuItem lecture = new JMenuItem ("Lecture de résultats d'analyse"); private JMenuItem etud_granulo = new JMenuItem("Etude granulométrique"); private JMenuItem diagrammes = new JMenuItem("Diagrammes"); private JMenu options = new JMenu("Options"); private JMenu adresses = new JMenu("Modifier"); private JMenuItem adresse_labo = new JMenuItem("Coordonnées du laboratoire"); private JMenuItem path_jrxml = new JMenuItem("Chemin d'accès au fichier de génération de rapport"); private JMenuItem fiches = new JMenuItem("Gérer les règles d'interprétation physique"); private JMenu programme = new JMenu("Programme"); private JMenuItem a_propos = new JMenuItem("A propos d'Atoll"); private JMenuItem disconnect = new JMenuItem("Se déconnecter"); private JMenuItem retour_accueil = new JMenuItem("Retour à l'accueil"); private JMenuItem quitter = new JMenuItem("Quitter Atoll"); public Atoll(JFrame parent, String title, boolean modal){ super(); this.setTitle(title); this.setSize(800, 600); this.setLocationRelativeTo(null); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.initComponent(); } private void initComponent(){ UIManager.getDefaults().put("Panel.background", new Color(0, 0, 0, 0)); // //Menu this.fonctions.add(lecture); this.fonctions.add(etud_granulo); this.fonctions.add(diagrammes); this.menuBar.add(fonctions); this.options.add(adresses); this.adresses.add(adresse_labo); this.adresses.add(path_jrxml); this.options.add(fiches); this.menuBar.add(options); this.programme.add(retour_accueil); this.programme.add(a_propos); this.programme.add(disconnect); this.programme.add(quitter); this.menuBar.add(programme); this.setJMenuBar(menuBar); this.a_propos.addActionListener(new aProposListener()); this.quitter.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent arg0) { System.exit(0);}}); this.retour_accueil.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent arg0) { getContentPane().removeAll(); initComponent(); validate();}}); this.disconnect.addActionListener(new disconnectListener()); this.path_jrxml.addActionListener(new PathListener()); this.fiches.addActionListener(new fichesListener()); this.analyse.addActionListener(new AnalyseListener()); Image img = new ImageIcon(TheMain.fichier[0]+"jardin1024.jpg").getImage(); generalPanel = new PanelJardin (img); ImageIcon imgicn = new ImageIcon (TheMain.fichier[0]+"Logo copie.png"); imgicn = new ImageIcon(resize(imgicn.getImage(),262,550)); iconLabel = new JLabel(imgicn); iconLabel.setBackground(new Color(0, 0, 0, 255)); Image icone = new ImageIcon(TheMain.fichier[0]+"icone.gif").getImage(); this.setIconImage(icone); JPanel center = new JPanel(); center.add(analyse); this.getContentPane().add(generalPanel); generalPanel.setLayout(new BorderLayout()); //generalPanel.add(analyse, BorderLayout.CENTER); generalPanel.add(center, BorderLayout.CENTER); generalPanel.add(iconLabel, BorderLayout.WEST); }
Le problème vient surement de mon PanelJardin (ligne 66 ou 67), car quand je le remplace par un JPanel simple, je n'ai plus ce problème (mais je perds le fond d'écran). Ceci dit, je comprends pas pourquoi il y a ce problème.
Si quelqu'un a une idée...







Répondre avec citation



Partager