Bonjour, j'essaye de mettre une image de fond sur un bouton mais cela ne fonctionne pas
j'ai donc crée sous ecliplse un projet qui s'appelle calculatrice,
j'ai pris une image que j'ai appellé vert.png je l'ai mis à la racine du dossier calculatrice de mon projet.
j'ai 3 classe, la classe fenetre , bouton et test
voici ma class Fenetre
la class bouton
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 import java.awt.BorderLayout; import java.awt.Color; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Fenetre extends JFrame{ private Panneau pan = new Panneau(); private Bouton bouton = new Bouton("mon bouton"); private JPanel container = new JPanel(); public Fenetre(){ this.setTitle("Animation"); this.setSize(300, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); container.setBackground(Color.white); container.setLayout(new BorderLayout()); container.add(pan, BorderLayout.CENTER); container.add(bouton, BorderLayout.SOUTH); this.setContentPane(container); this.setVisible(true); go(); }
et ma class qui appelle fenetre
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 import java.awt.Color; import java.awt.GradientPaint; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JButton; public class Bouton extends JButton{ private String name; private Image img; public Bouton(String str){ super(str); this.name = str; try { img = ImageIO.read(new File("vert.png")); } catch (IOException e) { e.printStackTrace(); } } public void paintComponent(Graphics g){ Graphics2D g2d = (Graphics2D)g; GradientPaint gp = new GradientPaint(0, 0, Color.blue, 0, 20, Color.cyan, true); g2d.setPaint(gp); // g2d.fillRect(0, 0, this.getWidth(), this.getHeight()); g2d.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this); g2d.setColor(Color.black); g2d.drawString(this.name, this.getWidth() / 2 - (this.getWidth() / 2 /4), (this.getHeight() / 2) + 5); } }
ici j'ai bien ma fenêtre avec un bouton mais l'image de fond n'y est pas,
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 public class Test { public static void main(String[] args){ Fenetre fen = new Fenetre(); } }
j'ai du faire une erreur mais lequel
merci vos aident .
Partager