【发布时间】:2013-07-23 22:11:13
【问题描述】:
我目前正在做一个记忆游戏项目,您可以在其中单击两张卡片来检查图片是否相同。我面临的问题是,如果两张卡没有相同的图片,则第二张图片不会出现,并且第一张图片也会关闭。
这是我认为有某种错误的代码。
public void actionPerformed(ActionEvent e)
{
clicks +=1;
Card clicked = (Card)e.getSource();
clicked.changeColor();
for(int i=0;i<16;i++)
if(clicked == cards.get(i))
openCardIndices.add(i);
if(clicks==2 && openCardIndices.get(openCardIndices.size()-1)!=openCardIndices.get(openCardIndices.size()-2))
{
if(cards.get(openCardIndices.get(openCardIndices.size()-1)).equals(cards.get(openCardIndices.get(openCardIndices.size()-2))))
{
(cards.get(openCardIndices.get(openCardIndices.size()-1))).removeActionListener(this);
(cards.get(openCardIndices.get(openCardIndices.size()-2))).removeActionListener(this);
}
else
{
openCardIndices.remove(openCardIndices.size()-1);
openCardIndices.remove(openCardIndices.size()-1);
//lockCards();
unlockCards();
}
clicks = 0;
}
【问题讨论】:
-
所以你只是要把你所有的代码都转储给我们然后说“修复它”?这不是堆栈溢出的工作方式。我们不是您的个人代码调试器 - 您实际上必须尝试一些事情!
-
我想发布所有代码没问题,但请给我们一个详细的解释,具体说明您认为问题可能出在哪里,以及任何可能相关的信息。
-
对不起,我不是那个意思,我从 2-3 个小时开始就在处理这个错误,并尝试了各种方法但仍然无法克服它,所以我想发布我的全部如果有人想尝试运行它以自己查看错误,代码可能会很好:s 我刚刚尝试评论 unlockCards() 并显示第二张卡,但程序不应该按顺序执行,为什么调用此方法会停止显示的图像,即使我在它之前使用 Thread.sleep ..
标签: java swing interface jbutton actionlistener