0

我一直在研究这个问题一段时间,但一直无法解决它。在这种情况memorygame下,每次您选择一个按钮,然后选择左上角的按钮(第一次选择该按钮时),左上角的按钮就会变为绿色。actionPerformed(ActionEvent e)这是因为当您第一次选择左上按钮和第二次选择时,public void 中的代码会执行两次。任何想法可能导致这种情况以及如何阻止它发生?

我确信有很多方法可以更好地创建一个memorygame.,但我讨厌在不知道出了什么问题的情况下继续前进。

我感谢您的帮助。

所有代码:

public class Alt3_3 extends JFrame implements ActionListener {

    JButton button[] = new JButton[52];
    String[] kort = {"POTATIS", "GLASS", "UNIX", "GLAS", "FOSTERS", "AIGH",
        "VAT 69", "SPIK", "FREDAG", "SITS", "FEST", "DaTe", "ALBIN",
        "42", "BOTTLE", "SANDELS", "DEW", "STOL", "PETSKI", "LAGER", "STOUT",
        "MALT", "EN RUTA", "BASS", "PrtScr", "DEL"};
    String[] svar1;
    String[] svar2;
    boolean firstVald;
    boolean green;
    int score = 0;
    int progress = 0;
    int index1;
    int index2;
    String svark1;
    String svark2;

    Alt3_3() {
        List<String> list1 = Arrays.asList(kort);
        Collections.shuffle(list1);
        svar1 = list1.toArray(new String[0]);
        List<String> list2 = Arrays.asList(kort);
        Collections.shuffle(list2);
        svar2 = list2.toArray(new String[0]);

        setLayout(new FlowLayout());
        setPreferredSize(new Dimension(650, 700));
        setTitle("Memorygame");

        for (int i = 0; i < button.length; i++) {
            button[i] = new JButton("");
            button[i].setPreferredSize(new Dimension(100, 50));
            add(button[i]);
            button[i].addActionListener(this);
        }

        pack();
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent e) {


        if (firstVald == false) {
            resetRed();
            firstVald = true;

            int index = -1;
            for (int i = 0; i < button.length; i++) {
                if (e.getSource() == button[i]) {
                    index = i;
                    break;
                }
            }
            index1 = index;

            if (index % 2 == 1) {
                button[index].setText(svar1[(index - 1) / 2]);
                svark1 = svar1[(index - 1) / 2];
            } else {
                button[index].setText(svar2[index / 2]);
                svark1 = svar2[index / 2];
            }
            button[index1].removeActionListener(this);

        } else {


            int index = -1;
            for (int i = 0; i < button.length; i++) {
                if (e.getSource() == button[i]) {
                    index = i;
                    break;
                }
            }
            index2 = index;

            if (index % 2 == 1) {
                button[index].setText(svar1[(index - 1) / 2]);
                svark2 = svar1[(index - 1) / 2];
            } else {
                button[index].setText(svar2[index / 2]);
                svark2 = svar2[index / 2];
            }

            if (svark1 == svark2) {
                progress++;
                green = true;
                button[index1].setBackground(Color.green);
                button[index2].removeActionListener(this);
                button[index2].setBackground(Color.green);
            } else {
                green = false;
                score++;
                button[index2].removeActionListener(this);
                button[index1].setBackground(Color.red);
                button[index2].setBackground(Color.red);

            }
            firstVald = false;

        }
        if (progress > 26) {
            showMessageDialog(null, "grattis" + Integer.toString(score));
            filhant.highScore(score);
            System.exit(0);
        }
    }

    public void resetRed() {
        if (green == false) {
            button[index1].setBackground(null);
            button[index2].setBackground(null);
            button[index1].setText("");
            button[index1].addActionListener(this);
            button[index2].setText("");
            button[index2].addActionListener(this);
        }
    }

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

1 回答 1

1

对于两个按钮中的第一个,您总是调用resetRed. 并且resetRed将指向由和addActionListener指示的按钮。由于这些初始化为零,您安装了三个动作监听器副本,并且代码不仅执行了两次,而且执行了三次:第一个作为不匹配对的第二个元素,第二个和第三个作为 a 的两个元素配对。避免第一次打电话。index1index2resetRed

于 2013-05-10T22:37:28.053 回答