Bonjour,
J'ai un tableau de boutons images et j'aimerai lorsque je clique sur un bouton changer l'image (passer d'un pion clair ("b1.gif") à un pion sombre ("b2.gif").
Quelqu'un aurait-il un idée?
Voici mon code:
package test; import java.awt.GridLayout; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; publicclass Fenetre extends JFrame { /** * */ privatestaticfinallong serialVersionUID = 1L; private JPanel pan = new JPanel(); // private JButton b = new JButton(new ImageIcon("b1.gif")); public Fenetre(){ this.setTitle("REVERSO"); this.setSize(450, 500); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setResizable(false); this.setLocationRelativeTo(null); setLayout(new GridLayout(5, 5, 5, 5)); // Ajout des boutons à notre contentPane JButton[] b = new JButton[25]; for(int i = 0; i < b.length; i++){ b[i] = new JButton(new ImageIcon("b1.gif")); b[i].addMouseListener(new MouseListener(){ @Override publicvoid mouseClicked(MouseEvent e){ // TODO Auto-generated method stub } @Override publicvoid mouseEntered(MouseEvent e){ // TODO Auto-generated method stub } @Override publicvoid mouseExited(MouseEvent e){ // TODO Auto-generated method stub } @Override publicvoid mousePressed(MouseEvent e){ // TODO Auto-generated method stub } @Override publicvoid mouseReleased(MouseEvent e){ // TODO Auto-generated method stub } }); pan.add(b[i]); } setContentPane(pan); } { this.setContentPane(pan); this.setVisible(true); } }
Bonne journée