Bonjour,
j'ai creer des JButton avec une image de fond:
Le probleme c'est que j'aimerais que lorsque j'appui sur le bouton, il y é un effet visuel.
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 public class BoutonAnotherColor extends JButton { /* Numero de serialisation par defaut. */ private static final long serialVersionUID = 1L; /* Image de fond après selection (format png)*/ private Image image; /** * Le constructeur de la classe. * @param string La chaîne de caractère identifiant * le bouton. * @param fichier Le chemin du fichier image. */ public BoutonAnotherColor(String string,String fichier) { super(); try { image = ImageIO.read(new File(fichier)); } catch (IOException e) { image = null; System.err.println("Fichier invalide"); } setFocusable(false); setText(string); setVisible(true); setContentAreaFilled(false); } /* * (non-Javadoc) * @see javax.swing.JComponent#paintComponent(java.awt.Graphics) */ public void paintComponent(Graphics g) { g.drawImage(image, 0, 0, null); super.paintComponent(g); } }
Il faut donc utiliser un mouseListener par exemple:
Le problème c'est que je cherche à ce que l'effet soit appliqué sur mon image de fond et non en affichant celui par defaut!
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 public void mousePressed(MouseEvent e) { if(e.getSource() instanceof BoutonAnotherColor) { ((BoutonAnotherColor)e.getSource()).setContentAreaFilled(false); ((BoutonAnotherColor)e.getSource()).getModel().setPressed(true); ((BoutonAnotherColor)e.getSource()).getModel().setArmed(true); ((BoutonAnotherColor)e.getSource()).repaint(); } }
Comment faire?
Partager