Problème addActionListener Java jeu Bubble
bonsoir,
j'ai un petit problème avec ActionPerformed ,je le maîtrise pas bien ,bref j'arrive pas a savoir pourquoi addactionlistener ne marche lorsque je l'utilise dans la méthode ActionPerformed ? :cry:
Code:
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Fenetre extends JFrame implements ActionListener {
JButton b[][];
int[][] grid = new int[10][10];
public int n = 10;
public int m = 10;
public Fenetre(){
this.setTitle("bubble");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
b = new JButton [n][m];
setLayout(new GridLayout(n,m));
for (int y = 0;y<m;y++){
for (int x = 0;x<n;x++){
Random rd = new Random();
int r = rd.nextInt(5);
switch (r){
case 0:
b[x][y] = new JButton("x= "+x+"y= "+y);
b[x][y].setBackground(Color.YELLOW);
b[x][y].addActionListener(this);
add(b[x][y]);
b[x][y].setEnabled(true);
b[x][y].setActionCommand(getName());
break;
case 1:
b[x][y] = new JButton("x= "+x+"y= "+y);
b[x][y].setBackground(Color.MAGENTA);
b[x][y].addActionListener(this);
add(b[x][y]);
b[x][y].setEnabled(true);
break;
case 2:
b[x][y] = new JButton("x= "+x+"y= "+y);
b[x][y].setBackground(Color.BLUE);
b[x][y].addActionListener(this);
add(b[x][y]);
b[x][y].setEnabled(true);
break;
case 3:
b[x][y] = new JButton("x= "+x+"y= "+y);
b[x][y].setBackground(Color.GREEN);
b[x][y].addActionListener(this);
add(b[x][y]);
b[x][y].setEnabled(true);
break;
case 4:
b[x][y] = new JButton("x= "+x+"y= "+y);
b[x][y].setBackground(Color.RED);
b[x][y].addActionListener(this);
add(b[x][y]);
b[x][y].setEnabled(true);
break;
case 5:
b[x][y] = new JButton("x= "+x+"y= "+y);
b[x][y].setBackground(Color.ORANGE);
b[x][y].addActionListener(this);
add(b[x][y]);
b[x][y].setEnabled(true);
break;
}
}
}
pack();
setVisible(true);
start();
}
public int getN(){
return(n);
}
public int getM(){
return(m);
}
public void start(){
Component temporaryLostComponent = null;
}
public static void main(String[] args) {
Fenetre window = new Fenetre();
window.setSize(600, 500);
}
@Override
public void actionPerformed(ActionEvent e) {
JButton c = (JButton)e.getSource();
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if(c == b[i][j]){
System.out.println("i= "+i+"j= "+j);
if(i<9 && b[i][j].getBackground() == b[i+1][j].getBackground()){
b[i+1][j].addActionListener(this);
remove(b[i+1][j]);
repaint();
}
else System.out.println("non");
if(i>0 && b[i][j].getBackground() == b[i-1][j].getBackground()){
b[i-1][j].addActionListener(this);
remove(b[i-1][j]);
repaint();
}
else System.out.println("non");
if(j<9 && b[i][j].getBackground() == b[i][j+1].getBackground()){
b[i][j+1].addActionListener(this);
remove(b[i][j+1]);
repaint();
}
else System.out.println("non");
if(j>0 && b[i][j].getBackground() == b[i][j-1].getBackground()){
b[i][j-1].addActionListener(this);
remove(b[i][j-1]);
repaint();
}
else System.out.println("non");
remove(b[i][j]);
repaint();
}
}
}
}
} |
Normalement lorsqu'il trouve un bouton de même couleur et adjacent il le supprime!!mais dans ce code il ne le supprime pas!! pourquoi?
et merci d'avance!! ;)