Ajouter une image de fond à une JPanel


Voici une fenêtre avec un fond personnalisé.




















Et voici le code correspondant:

import java.awt.Graphics;
import java.awt.Toolkit;

import javax.swing.JFrame;

public class Fenetre extends JFrame {
   
    private String image = "image.jpg";
   
    public Fenetre() {
        this.setSize(400,400);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
   
    @Override
    public void paint(Graphics arg0) {
        arg0.drawImage(Toolkit.getDefaultToolkit().getImage(image),0,0,null);
    }
   
    /**
     * @param args
     */
    public static void main(String[] args) {
        new Fenetre();
    }

}

Laboratoire SUPINFO des technologies Sun
labo-sun@supinfo.com


Conditions d'utilisation et © Copyright SUPINFO International University
23, rue de Château Landon - 75010 PARIS - Tél : +33 (0) 153359700 Fax : +33 (0) 153359701
Respect de la vie privée