Bonjour,
Utilisant netbean et son utilitaire de design, je souhaite rajouter une image à ma frame.
Pour cela, j'ai dérivée une classe du JPanel, et l'intègre dans mon gridlayout.

Jusque là, et même si je suis pas fier de mon code, ça marche.
Mais je souhaite rajouter une bordure à mon image. Et là c'est l'échec.
Quelqu'un saurait où j'ai manqué une marche?

Merci !

Panel contenant l'image :
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
 
package catalogue;
 
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
 
/**
 *
 * @author Olivier
 */
public class ImagePanel extends JPanel
{
    private String imagePath;
    private static String defaultImagePath = "image/Image_manquant.png";
    private Image image = null;
 
    public boolean setImage(String newPath)  
    {
        File myFile = new File(newPath);
        if (myFile.exists())
        {
            image = getToolkit().getImage(newPath);
            return true;
        }
        return false;
    }
 
    public ImagePanel() throws IOException
    {
        super();
        this.setBorder(BorderFactory.createLineBorder(Color.black));
        setVisible(true);
        File myFile = new File(defaultImagePath);
        if (myFile.exists())
        {
            imagePath = defaultImagePath;
        }
        else 
            throw new IOException("Image File doesn't exist");
 
        setMaximumSize(new java.awt.Dimension(400, 400));
        setPreferredSize(new java.awt.Dimension(400, 400));
    }
 
    @Override
    public void paint(Graphics g)
    {
        super.paintComponent(g);
        image = getToolkit().getImage(imagePath);
        if(image != null) // Si l'image existe, ...
        {
            g.drawImage(image,0,0, this.getWidth(),this.getHeight(), this); // ... on la dessine
            setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        }   
    }
 
}
L'ajout dans le JFrame :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
 
try {
            initComponents();
            ImagePanel imagePanel = new ImagePanel();
 
            getContentPane().add(imagePanel, java.awt.BorderLayout.WEST);
            pack(); 
 
        } catch (IOException ex) {
            Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }