为什么我的第一个CardClicked.setDisabledIcon(img)能够工作,但我的第二个CardClicked.setDisabledIcon(img)不起作用?
问题描述:
我正在构建一个Java Swing记忆游戏。这个想法是点击第一张牌,它将图像从baseImage更改为另一张图像。当点击第二张卡片时,它应该做同样的事情,等待几秒钟,然后如果它们不匹配,则将所有事件重置回基地,或者如果它们一起翻转,则将它们重置为基地。为什么我的第一个CardClicked.setDisabledIcon(img)能够工作,但我的第二个CardClicked.setDisabledIcon(img)不起作用?
现在,如果它们不匹配,第二张卡永不改变图像。以下是相关的代码。应该指出的是,如果他们确实匹配,那么图像就会显示出来,并且在世界上都是正确的。
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");
}
}
答
看起来好像有几个问题在同一时间工作。 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();
}
}
不要在事件处理中使用'Thread.sleep(int)'。它阻止了GUI。 –
请同时提供一个可运行的简短示例([MVCE](http://stackoverflow.com/help/mcve))。 –
@SergiyMedvynskyy作为一个初学者,我试图远离'Thread.sleep(int)',因为它与Swing有关的问题。不幸的是,我无法想出正确实现Swing Timer的方法,并且它运行得很好。我会尽力在明天把这个分解成MVCE。欣赏一切。 –