Bonsoir,

Je n'arrive pas à centrer des images dans un GridPanel. Voilà 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
 
package fr.grandmonarque.antoine.poker;
 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
 
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
 
public class PokerMain extends JFrame {
 
    private static final long serialVersionUID = 1L;
 
    public PokerMain() throws IOException {
 
        setLayout(new GridLayout(3,1));
 
        JLabel center = new JLabel("");
 
        final BufferedImage image = ImageIO.read(new File("C:\\Users\\Utilisateur\\Downloads\\cartefm100x62.jpg"));
        JLabel icon1 = new JLabel(new ImageIcon(image));
        JLabel icon2 = new JLabel(new ImageIcon(image));
        JLabel icon3 = new JLabel(new ImageIcon(image));
        JLabel icon4 = new JLabel(new ImageIcon(image));
        JPanel north = new JPanel();
        north.add(icon1, BorderLayout.CENTER);
        north.add(icon3, BorderLayout.CENTER);
        north.setBackground(Color.blue);
        JPanel south = new JPanel();
        south.add(icon2, BorderLayout.CENTER);
        south.add(icon4, BorderLayout.CENTER);
        south.setBackground(Color.red);
 
        getContentPane().add(north, BorderLayout.CENTER);
        getContentPane().add(center);
        getContentPane().add(south, BorderLayout.CENTER);
 
        pack();
        validate();
 
        setVisible(true);
        setSize(700, 600);
        setLocationRelativeTo(null);
 
} 
 
 
    public static void main(String[] args) throws IOException {
        new PokerMain();
    }
 
}
Et voilà ce que ça donne :

Nom : pok.png
Affichages : 67
Taille : 51,9 Ko