Bonjour à tous !

Je débute depuis 3/4 jours avec les interfaces graphiques en JAVA.
Je souhaite insérer une image dans une fenêtre, mais apparemment, il y a une faute dans mon code.

Voici mon code (Si vous voyez quelques améliorations à y apporter, merci de me les signaler):

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
public class Main {
        public static void main(String[] args) {
                FenetreManager fenetreManager = new FenetreManager();
                        fenetreManager.appelFenetre();
        }
 
}
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
import java.awt.Color;
 
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Fenetre extends JFrame{   
 
        //Variables
        private String titre;
        private int xSize;
        private int ySize;
        private int xPosition;
        private int yPosition;
 
        //Constructeur
        public Fenetre(String titre, int xSize, int ySize, int xPosition, int yPosition) {
                //Titre de la fenêtre
            this.setTitle(titre);
            //Taille de la fenêtre
            this.setSize(xSize, ySize);
            //Emplacement
            this.setLocation(xPosition, yPosition);
            //Fin de l'application lors de la fermeture de la fenêtre
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
            //Rend visible la fenêtre
            this.setVisible(true);
            //Rend possible le redimensionnement de la fenêtre
            this.setResizable(true);
             //Instanciation d'un objet JPanel
            JPanel panel = new JPanel();
            //Définition de sa couleur de fond
            panel.setBackground(Color.ORANGE);        
            //On prévient notre JFrame que notre JPanel sera son content pane
            this.setContentPane(new Panneau());
            //On le définit comme visible
            this.setVisible(true);
            //Insertion d'une image
            JLabel labelImage = new JLabel();
            //Chargement de l'image
            labelImage.setIcon(new ImageIcon("C:/j0099165.jpg"));
            //Ajout de l'image
            panel.add(labelImage);
        }
 
        //Getters & Setters
        public String getTitre() {
                return titre;
        }
 
        public void setTitre(String titre) {
                this.titre = titre;
        }
 
        public int getxSize() {
                return xSize;
        }
 
        public void setxSize(int xSize) {
                this.xSize = xSize;
        }
 
        public int getySize() {
                return ySize;
        }
 
        public void setySize(int ySize) {
                this.ySize = ySize;
        }
 
        public int getxPosition() {
                return xPosition;
        }
 
        public void setxPosition(int xPosition) {
                this.xPosition = xPosition;
        }
 
        public int getyPosition() {
                return yPosition;
        }
 
        public void setyPosition(int yPosition) {
                this.yPosition = yPosition;
        }      
}
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
import javax.swing.JFrame;
 
 
public class FenetreManager extends JFrame {
 
        //Constructeur
        public FenetreManager() {
        }
 
        //Instanciation d'un objet de type Fenetre
        public void appelFenetre(){
                new Fenetre("Titre de ma fenêtre", 150, 150, 500, 500);
        }
 
}
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import java.awt.Font;
 
public class Panneau extends JPanel {
  public void paintComponent(Graphics g){
    g.fillOval(this.getWidth()/4, this.getHeight()/4, this.getWidth()/2, this.getHeight()/2);
    Font font = new Font("Courier", Font.BOLD, 20);
    g.setFont(font);
    g.setColor(Color.red);          
    g.drawString("Titre de ma fenêtre", this.getWidth()/3/2, 20);          
  }
}