簡體   English   中英

動作偵聽器actionPerformed通過代碼做兩次

[英]Action listener actionPerformed does through code double

我已經花了一段時間研究這個齒輪,但是還無法解決。 在此memorygame ,每次您選擇一個按鈕,然后每次選擇左上方的按鈕(第一次選擇該按鈕)時,左上方的按鈕都會變為綠色。 這是因為,無論您是第一次還是第二次選擇左上方按鈕,公共void actionPerformed(ActionEvent e)的代碼都會執行兩次。 有什么想法可能導致這種情況,如何阻止這種情況發生?

我確信有很多方法可以更好地創建一個memorygame ,但是我討厭繼續前進,不知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();
    }
}

對於兩個按鈕中的第一個,您始終調用resetRed 然后resetRed將把addActionListenerindex1index2指示的按鈕上。 由於將它們初始化為零,因此您安裝了動作偵聽器的三個副本,代碼不僅執行兩次,而且執行三次:第一個作為不匹配對的第二個元素,第二個和第三個作為a的兩個元素。配對。 避免第一次調用resetRed

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM