Ajouter un commentaire

Niroken

Hello,

Je te passe un morceau de code qui te permet de changer l icone du boutton en cliquant dessus :

package graphic;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;

public class TestJButtonIconFrame extends JFrame {

    private static final long serialVersionUID = 1L;
    
    private JButton mButton;
    
    private ImageIcon mImageIconPionClair;
    private ImageIcon mImageIconPionFonce;
    
    public TestJButtonIconFrame () {
        setLayout(new FlowLayout());
        setSize(new Dimension(100, 100));
        
        mImageIconPionClair = new ImageIcon("b1.gif");
        mImageIconPionFonce = new ImageIcon("b2.gif");
        
        mButton = new JButton(mImageIconPionClair);
        mButton.setPreferredSize(new Dimension(20, 20));
        mButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent pE) {
                ImageIcon mImageIconButton = (ImageIcon) mButton.getIcon();
                
                if (mImageIconButton.getDescription().equals(mImageIconPionClair.getDescription())) {
                    mButton.setIcon(mImageIconPionFonce);
                } else if (mImageIconButton.getDescription().equals(mImageIconPionFonce.getDescription())) {
                    mButton.setIcon(mImageIconPionClair);
                }
            }
            
        });
        
        add(mButton);
                
        setVisible(true);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
    
}

Bonne chance,
Niroken

Filtered HTML

Plain text

CAPTCHA
Cette question permet de vérifier que vous n'êtes pas un robot spammeur :-)
 K  K  H  H  ZZZZZ  PPPP   U   U 
K K H H Z P P U U
KK HHHH Z PPPP U U
K K H H Z P U U
K K H H ZZZZZ P UUU