我正在制作纸牌匹配游戏,但在翻牌时遇到困难。出于某种原因,无论我单击哪张卡,最后一张卡都会翻转,我不知道为什么会这样。我希望所有卡都可以翻转。
下面是我目前拥有的代码,希望有人可以帮助我解决问题所在。
谢谢!
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import java.awt.Image;
public class PlayingScreen extends JPanel {
public static int cardonTable[] = new int [54];
private CardButton cardonTableButton[] = new CardButton[54];
int cardFlipAllowed = 2;
public static boolean cardMatched;
public static int clicked1;
public static int clicked2;
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();
}
int j = i;
cardonTableButton[i].addActionListener(new ActionListener () {
@Override
public void actionPerformed(ActionEvent e) {
if(cardFlipAllowed>0){
// retrieve CardButton from event
CardButton cardButton = (CardButton) e.getSource();
if(cardFlipAllowed==2) {
clicked1 = j ;
}
else {
clicked2 = j ;
Matched.matched(clicked1+1,clicked2+1); //see if they matched
System.out.println(clicked1);
System.out.println(clicked2);
System.out.println(cardMatched);
}
// Flip card. This method handles the logic of updating
// flipped and changing the icon
cardButton.flip();
}
cardFlipAllowed--;
}
});
}
}
}
public class Matched {
public static void matched(int cardNum1, int cardNum2) {
//list of matches
if ( (cardNum1==53&&cardNum2==54)||(cardNum1==54&&cardNum2==53) ) {
PlayingScreen.cardMatched = true;
}
else if( (cardNum1<=13&& (cardNum2==cardNum1+13) ) || (cardNum2<=13&& (cardNum1==cardNum2+13) ) ) {
PlayingScreen.cardMatched = true;
}
else if( (cardNum1>=27&& (cardNum2==cardNum1+13) ) || (cardNum2>=27&& (cardNum1==cardNum2+13) ) ) {
PlayingScreen.cardMatched = true;
}
else {
PlayingScreen.cardMatched = false;
}
//1-14, 13-26
//27-40. 39-52
}
}
答案 0 :(得分:2)
在每个按钮上调用事件侦听器时,变量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,并允许您使用getters / setters存储正面和背面图像,并使用flip()
方法管理其翻转状态。这样可以简化代码,并为您提供在方式和位置上更新卡图像状态的更多灵活性。
您的actionPerformed()
方法现在仅调用创建事件的CardButton的flip()
方法。所有逻辑都封装在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);
}
}
播放屏幕
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();
}
});
}
}
}