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
|
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
public class BoutonImageAction extends JFrame implements ActionListener {
JLabel etiquette=new JLabel("cliquez pour changer d'image");
Icon Image1=new ImageIcon("monImage.gif");
Icon Image2=new ImageIcon("graffiti nyc 8.gif");
JButton SwingButton=new JButton(Image1);
BoutonImageAction(String titre){
super(titre);
FlowLayout fl=new FlowLayout();
Container cp=getContentPane();
cp.setLayout(fl);
SwingButton.addActionListener(this);
cp.add(etiquette);
cp.add(SwingButton);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==SwingButton)
{
if(SwingButton.getIcon()==Image1)
SwingButton.setIcon(Image2);
else SwingButton.setIcon(Image1);
}
}
public static void main(String[] args) {
BoutonImageAction BIA=new BoutonImageAction("MonBia");
BIA.setSize(900, 900);
BIA.setVisible(true);
System.out.println(new File("monimage.gif").exists());
System.out.println(new File("").getAbsolutePath());
}
} |
Partager