繁体   English   中英

在 java 中创建 memory 游戏时需要帮助解决错误

[英]need help solving errors while creating a memory game in java

我是 java 的新手,对我的代码中的错误感到非常困惑。 我在第 48 行出现错误“从内部 class 引用的局部变量必须是最终的或有效的最终”。 这是我的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MemoryGame extends JFrame 
{
    private JButton[][] buttons = new JButton[4][4];
    private Color[] colors = {Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW};
    private List<Color> colorList = new ArrayList<>();
    private int score = 0;

    public MemoryGame() 
    {
        setTitle("Memory Game");
        setSize(400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(4, 4));

        // populate colorList with two copies of each color
        for (int i = 0; i < 2; i++) 
        {
            for (Color color : colors) 
            {
                colorList.add(color);
            }
        }
        // shuffle colorList
        Collections.shuffle(colorList);

        int index = 0;
        for (int i = 0; i < 4; i++) 
        {
            for (int j = 0; j < 4; j++) 
            {
                JButton button = new JButton();
                button.setBackground(Color.GRAY);
                buttons[i][j] = button;
                add(button);
                button.addActionListener(new ActionListener() 
                {
                    @Override
                    public void actionPerformed(ActionEvent e) 
                    {
                        final int currentIndex = index;
                        JButton clickedButton = (JButton) e.getSource();
                        Color color = colorList.get(currentIndex);
                        clickedButton.setBackground(color);
                        clickedButton.setEnabled(false);
                        for (int x = 0; x < 4; x++)
                        {
                            for (int y = 0; y < 4; y++)
                            {
                                if (buttons[x][y].getBackground() == color && buttons[x][y] != clickedButton) 
                                {
                                    buttons[x][y].setEnabled(false);
                                    score++;
                                    if (score == 8) 
                                    {
                                        JOptionPane.showMessageDialog(MemoryGame.this, "You won!");
                                        System.exit(0);
                                    }
                                }
                            }
                        }
                    }
                });
            index++;
        }
    }
    setVisible(true);
    }

    public static void main(String[] args) 
    {
        new MemoryGame();
    }
}

我尝试了不同的方法来解决错误,但它们只会导致更多错误。 该程序旨在具有不同方块的网格,可以单击这些方块以显示其 colors 并由用户匹配。 当用户匹配网格上的所有 colors 时,他们获胜。

问题出在final int currentIndex = index; 正如编译器在错误消息中告诉您的那样。 actionPerformed将在某个未指定的未来时间运行,并且编译器不知道您希望在那时找到什么index值。

你想让我做什么? 将 colors 分配给按钮。 你可以更直接地做到这一点

JButton button = new JButton();
button.setBackground(Color.GRAY);
buttons[i][j] = button;
// let's store the color for later
button.putClientProperty("color", colorList.get(index));
add(button);

在处理程序之外。

然后在里面检索它:

public void actionPerformed(ActionEvent e) 
{
    JButton clickedButton = (JButton) e.getSource();
    Color color = (Color)clickedButton.getClientProperty("color");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM