В то время, когда ваш слушатель событий вызывается для каждой кнопки, переменная j = 53. Таким образом, каждая из ваших кнопок устанавливает значок в 54-й кнопке. Вместо этого вы хотите изменить значок кнопки, вызвавшей событие.
Сделайте это, изменив код в actionPerformed()
методе:
try {
// retrieve JButton from event
JButton btn = (JButton) e.getSource();
// set icon
btn.setIcon(new ImageIcon (ImageIO.read(this.getClass().getResource("OVO_BACK.png"))));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ОБНОВЛЕНИЕ:
Я включил код ниже для пользовательского класса CardButton и изменил класс PlayingScreen. CardButton оборачивает JButton и позволяет сохранять переднее и заднее изображения с помощью методов получения / установки и управлять его перевернутым состоянием, используя метод flip()
. Это упрощает код и дает вам больше гибкости в том, как и где вы обновляете состояние изображения вашей карты.
Ваш метод actionPerformed()
теперь просто вызывает метод flip()
CardButton, который создал событие. Вся логика инкапсулирована в классе CardButton и отделена от класса PlayingScreen.
CardButton
public class CardButton extends JButton {
private boolean flipped;
private ImageIcon backIcon;
private ImageIcon frontIcon;
public CardButton() {
super("");
}
/*
* Flip the card
*/
public void flip() {
if (flipped) {
setFlipped(false);
} else {
setFlipped(true);
}
}
public boolean isFlipped() {
return flipped;
}
/*
* Updates flipped property and changes icon
* based on flipped property
*/
private void setFlipped(boolean flipped) {
this.flipped = flipped;
// show back icon when flipped = true
if (flipped) {
showBackIcon();
// otherwise show front icon
} else {
showFrontIcon();
}
}
public ImageIcon getBackIcon() {
return backIcon;
}
public void setBackIcon(ImageIcon backIcon) {
this.backIcon = backIcon;
// set icon in parent JButton
super.setIcon(backIcon);
}
public void showBackIcon() {
super.setIcon(backIcon);
}
public ImageIcon getFrontIcon() {
return frontIcon;
}
public void setFrontIcon(ImageIcon frontIcon) {
this.frontIcon = frontIcon;
// set icon in parent JButton
super.setIcon(frontIcon);
}
public void showFrontIcon() {
super.setIcon(frontIcon);
}
}
PlayingScreen
public class PlayingScreen extends JPanel {
private static int j = 0;
private static int cardonTable[] = new int [54];
private CardButton cardonTableButton[] = new CardButton[54];
public PlayingScreen() {
// variables used in loop
Image dimg = null;
ImageIcon dimgIcon = null;
setLayout(null);
CardDeal.createDeck(); //create a deck
for(int i=0;i<54;i++) {
cardonTable[i] = CardDeal.cardDeal();
cardonTableButton[i] = new CardButton();
if(i/9<=0) cardonTableButton[i].setBounds(50+i*100+i*20, 50, 100, 150);
else if(i/9<=1) cardonTableButton[i].setBounds(50+(+i-9)*100+(i-9)*20, 200+20, 100, 150);
else if(i/9<=2) cardonTableButton[i].setBounds(50+(i-18)*100+(i-18)*20, 350+40, 100, 150);
else if(i/9<=3) cardonTableButton[i].setBounds(50+(i-27)*100+(i-27)*20, 500+60, 100, 150);
else if(i/9<=4) cardonTableButton[i].setBounds(50+(i-36)*100+(i-36)*20, 650+80, 100, 150);
else if(i/9<=5) cardonTableButton[i].setBounds(50+(i-45)*100+(i-45)*20, 800+100, 100, 150);
add(cardonTableButton[i]);
BufferedImage img = null;
try {
img = ImageIO.read(this.getClass().getResource((cardonTable[i])+".png"));
} catch (IOException e) {
e.printStackTrace();
}
//convert BufferedImage to ImageIcon
dimg = img.getScaledInstance(cardonTableButton[i].getWidth(), cardonTableButton[i].getHeight(), Image.SCALE_SMOOTH);
dimgIcon = new ImageIcon(dimg);
// set front icon
cardonTableButton[i].setFrontIcon(dimgIcon);
try {
// set back icon
cardonTableButton[i].setBackIcon(new ImageIcon (ImageIO.read(this.getClass().getResource("OVO_BACK.png"))));
} catch (IOException e) {
e.printStackTrace();
}
cardonTableButton[i].addActionListener(new ActionListener () {
@Override
public void actionPerformed(ActionEvent e) {
// retrieve CardButton from event
CardButton cardButton = (CardButton) e.getSource();
// Flip card. This method handles the logic of updating
// flipped and changing the icon
cardButton.flip();
}
});
}
}
}