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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| package test;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Fenetre extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel pan = new JPanel();
protected ImageIcon b1;
protected ImageIcon b2;
public Fenetre() {
this.setTitle("Test");
this.setSize(450, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocationRelativeTo(null);
setLayout(new GridLayout(5, 5, 0, 0));
JButton[] b = new JButton[25];
for (int i = 0; i < b.length; i++) {
b1 = new ImageIcon("b1.gif");
b2 = new ImageIcon("b2.gif");
b[i] = new JButton(b1);
b[i].addActionListener(this);
pan.add(b[i]);
}
setContentPane(pan);
}
{
this.setContentPane(pan);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JButton b = (JButton) (e.getSource());
ImageIcon mImageIconButton = (ImageIcon) b.getIcon();
if (mImageIconButton.getDescription().equals(b1.getDescription())) {
b.setIcon(b2);
} else if (mImageIconButton.getDescription()
.equals(b2.getDescription())) {
b.setIcon(b1);
}
}
} |
Partager