Hello everyone. I want to change the background color of the button when I click it.
the problem now is the only the last button works, but not the way I want to.(It changes color I click other buttons.)
here is my button code.
for (int i=0; i<(5; i++) {
for (int c=0; c <5; c++){
button=new JButton("");
button.setBackground(Color.WHITE);
button.setForeground(Color.BLACK);
button.addMouseListener(new ButtonListener());
panel1.add(button);
}
}
here is my listener code.
public class ButtonListener extends MouseAdapter{
public void mouseClicked(MouseEvent e){
if(x==0){
button.setBackground(Color.BLACK);
x=1;
System.out.println("00000000000000");
}
else if(x==1){
button.setBackground(Color.WHITE);
x=0;
System.out.println("1111111111111111");
}
}
Thanks