我正在构建一个Java Swing Memory Game。想法是点击第一张卡片,它将图像从baseImage更改为另一张图片。当点击第二张牌时,它应该做同样的事情,等待几秒钟,然后将所有东西重置回基地,如果他们不匹配或者如果他们这样做就将它们翻转过来。
现在,如果它们不匹配,则第二张卡永远不会更改图像。以下是相关代码。应该注意的是,如果它们匹配,图像显示并且一切都在世界上是正确的。
public void cardClickHandler(ActionEvent ae) {
JButton card = new JButton();
card = (JButton) ae.getSource();
// disable the card that was clicked
card.setEnabled(false);
// behavior for first card clicked
if (clickCounter == 0) {
firstCardClicked = card;
img = new ImageIcon("images2/img" + firstCardClicked.getName() + ".jpg");
firstCardClicked.setDisabledIcon(img);
System.out.println("Button " + firstCardClicked.getName() + " clicked!");
clickCounter++;
}
// behavior for second card clicked
else {
secondCardClicked = card;
img = new ImageIcon("images2/img" + secondCardClicked.getName() + ".jpg");
secondCardClicked.setDisabledIcon(img);
System.out.println("Button " + secondCardClicked.getName() + " clicked!");
clickCounter--;
}
// behavior if two cards have been clicked and they match
if (firstCardClicked.getName().equals(secondCardClicked.getName()) && clickCounter == 0) {
// player turn control and scoring
if (p1Turn) {
p1NumScore++;
p1Score.setText(Integer.toString(p1NumScore));
scorePanel.revalidate();
System.out.println("Good job Mike, got a pair!");
p1Turn = !p1Turn;
}
else {
p2NumScore++;
p2Score.setText(Integer.toString(p2NumScore));
scorePanel.revalidate();
System.out.println("Good job Peanut, got a pair!");
p1Turn = !p1Turn;
}
}
// behavior if two cards have been clicked and they do not match
else if (!(firstCardClicked.getName().equals(secondCardClicked.getName())) && clickCounter == 0) {
// keep cards flipped for a few seconds
try {
System.out.println("Before Sleep"); // testing
Thread.sleep(2000);
System.out.println("After Sleep"); // testing
}
catch (Exception e) {
e.printStackTrace();
}
// enable the cards and reset images
firstCardClicked.setEnabled(true);
firstCardClicked.setIcon(baseImage);
secondCardClicked.setEnabled(true);
secondCardClicked.setIcon(baseImage);
// change turns
p1Turn = !p1Turn;
System.out.println("Keep Playing");
}
}
答案 0 :(得分:0)
看起来似乎有一些问题同时发生。 Thread.sleep()
与.setEnabled(true)
命令一起造成了严重破坏。对象的状态有问题所以我需要在代码的末尾清除它们。 .setDisabledIcon()
方法存在一些已知问题,此处的许多帖子都要求.setIcon()
在其之前。以下是与正在使用的javax.swing.Timer
一起使用的固定代码。
public void cardClickHandler(ActionEvent ae)
{
// method variables
JButton card = (JButton) ae.getSource();
boolean clearState = false;
// behavior for first card clicked
if (isFirstCard)
{
firstCardClicked = card;
// a known issue with "setDisabledIcon()" required the "setIcon()" method
firstCardClicked.setIcon(new ImageIcon("images2/img" + firstCardClicked.getName() + ".jpg"));
firstCardClicked.setDisabledIcon(new ImageIcon("images2/img" + firstCardClicked.getName() + ".jpg"));
// disable the flipped card
firstCardClicked.setEnabled(false);
// indicate the next card clicked is the second card
isFirstCard = false;
}
// behavior for second card clicked
else
{
secondCardClicked = card;
// a known issue with "setDisabledIcon()" required the "setIcon()" method
secondCardClicked.setIcon(new ImageIcon("images2/img" + secondCardClicked.getName() + ".jpg"));
secondCardClicked.setDisabledIcon(new ImageIcon("images2/img" + secondCardClicked.getName() + ".jpg"));
// disable the flipped card
secondCardClicked.setEnabled(false);
// indicate the next card clicked is the first card again
isFirstCard = true;
// indicate to the system both cards have been clicked and can clear objects
clearState = true;
}
// behavior if two cards have been clicked and they match
if (isFirstCard && firstCardClicked.getName().equals(secondCardClicked.getName()))
{
// player turn control and scoring
if (p1Turn)
{
p1NumScore++;
p1Score.setText(Integer.toString(p1NumScore));
scorePanel.revalidate();
p1Turn = !p1Turn;
}
else
{
p2NumScore++;
p2Score.setText(Integer.toString(p2NumScore));
scorePanel.revalidate();
p1Turn = !p1Turn;
}
}
// behavior if two cards have been clicked and they do not match
else if (isFirstCard && !(firstCardClicked.getName().equals(secondCardClicked.getName())) )
{
// enable the cards and reset images
firstCardClicked.setIcon(BASE_IMAGE);
secondCardClicked.setIcon(BASE_IMAGE);
// change turns
p1Turn = !p1Turn;
}
if (clearState)
{
javax.swing.Timer timer = new javax.swing.Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent ae) {
firstCardClicked.setEnabled(true);
secondCardClicked.setEnabled(true);
isFirstCard = true;
firstCardClicked = null;
secondCardClicked = null;
}
});
// Normally the timer's actionPerformed method executes repeatedly. Tell it only to execute once.
timer.setRepeats(false);
// Start the timer.
timer.start();
}
}