我有这段代码
for (int j = 0; j < 2; j++) {
// Shuffle images array
Collections.shuffle(Arrays.asList(cards));
for (int i = 0; i < cards.length; i++) {
// Create new instance of card
final Card card = new Card(cards[i].getCardID(), cards[i].getCardImage());
ImageIcon icon = card.getDefIcon();
card.setIcon(icon);
this.add(card);
card.addMouseListener(new MouseAdapter()
{
card.clicked = true;
public void mouseReleased(MouseEvent e)
{
}
});
}
}
但是“card.clicked = true”的行有错误 令牌“clicked”上的语法错误,此令牌后的VariableDeclaratorld
Card类看起来像这样
public class Card extends JLabel {
int cardID;
public boolean clicked = false;
BufferedImage cardImage;
ImageIcon defIcon = new ImageIcon("E:/Java Projects/UUR - Semestralka/resources/card.png");
Card (int cardID, BufferedImage cardImage) {
this.cardID = cardID;
this.cardImage = cardImage;
}
public int getCardID() {
return cardID;
}
public BufferedImage getCardImage() {
return cardImage;
}
public ImageIcon getDefIcon() {
return defIcon;
}
}
有谁知道我做错了什么?
答案 0 :(得分:2)
你需要把
card.clicked = true;
方法内部如下
public void mouseReleased(MouseEvent e)
{
card.clicked = true;
}
答案 1 :(得分:1)
card.clicked = true
必须进入方法正文(在void mouseReleased(...)
内)
答案 2 :(得分:1)
您应该将其移至匿名mouseReleased
MouseAdapter
方法
答案 3 :(得分:0)
card.addMouseListener(new MouseAdapter() {
//card.clicked = true; <-- cant be here
public void mouseReleased(MouseEvent e) {
card.clicked = true; //should go here
}
});