ActionListener中的GUI内存游戏(不能成对)

时间:2017-05-27 07:41:38

标签: java actionlistener

所以我的问题是setHit方法不起作用,如果我得到一个匹配它不会识别它并且letUnclick方法运行 我只想将按钮setText设置为HIT,当它们匹配时,如果不是继续 我究竟做错了什么?

import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.Timer;

public class Memory_Game extends JFrame {

    ArrayList<ImageIcon> allCards = new ArrayList<ImageIcon>();
    ImageIcon cover;
    ImageIcon[] pics;
    JToggleButton[] buttons;
    Timer myTimer;

    int index;

    public Memory_Game(String text) {
        super(text);
        Container cp = getContentPane();
        cp.setLayout(new GridLayout(4, 4));

        setDefaultCloseOperation(EXIT_ON_CLOSE);

        // muss Togglebuttons in arraylist einfügen
        pics = new ImageIcon[16];
        buttons = new JToggleButton[16];
        for (int i = 0; i < 16; i++) {

            cover = new ImageIcon(getClass().getResource("Pokeball.PNG"));
            cover.setImage(cover.getImage().getScaledInstance(150, -1, Image.SCALE_SMOOTH));

            pics[i] = new ImageIcon(getClass().getResource("pic" + i % 8 + ".png"));
            pics[i].setImage(pics[i].getImage().getScaledInstance(150, -1, Image.SCALE_SMOOTH));

            buttons[i] = new JToggleButton();
            // buttons[i].setSize(200, 200);
            buttons[i].setIcon(cover);
            // buttons[i].setText("KEK");

            allCards.add(pics[i]);

        }
        Collections.shuffle(allCards);
        for (int i = 0; i < 16; i++) {
            buttons[i].setSelectedIcon(allCards.get(i));
            buttons[i].addActionListener(new Click());

            cp.add(buttons[i]);

        }
        setIconImage(cover.getImage());

    }
//method to get the 2 selected buttons with icon in an array in order to 
//compare them in the actionlistener (if case == 2)
// public JToggleButton[] getClicked() {

        int index = 0;
        JToggleButton[] klicked = new JToggleButton[2];

        for (int i = 0; i < allCards.size(); i++) {
            if (buttons[i].isSelected()) {
                klicked[index] = buttons[i];
                index++;
            }

        }
        return klicked;

    }
//counting how many buttons are clicked
    public int countClicked() {

        int count = 0;
        for (int i = 0; i < allCards.size(); i++) {
            if (buttons[i].isSelected()) {
                count++;
            }

        }
        return count;

    }
// if match let them open and put the word HIT on these buttons
    public void setHit() {

        for (int i = 0; i < allCards.size(); i++) {
            if (buttons[i].isSelected()) {
                buttons[i].setSelected(true);
                buttons[i].setText("HIT");

            }
        }

    }
//if no match make them normal again
    public void letUnclicked() {

        for (int i = 0; i < allCards.size(); i++) {
            if (buttons[i].isSelected()) {
                buttons[i].setSelected(false);

            }
        }

    }

    private class Click implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            JToggleButton btn = (JToggleButton) e.getSource();

            if (countClicked() < 2) {
                btn.setSelected(true);
            } else if (countClicked() == 2) {
                JToggleButton[] klicked = getClicked();
                if (klicked[0].getSelectedIcon() == klicked[1].getSelectedIcon()) {

                    Timer timer;
                    timer = new Timer(500, new ActionListener() {
                        public void actionPerformed(ActionEvent ae) {

                            setHit();

                            // for (int i = 0; i < klicked.length; i++) {
                            // for (int j = 0; j < allCards.size(); i++) {
                            // if (buttons[j].equals(klicked[i])) {
                            // buttons[j].setText("HIT BREH");
                            // }
                            // }
                            // }
                        }
                    });
                    timer.setRepeats(false);
                    timer.start();

                } else if (klicked[0].getSelectedIcon() != klicked[1].getSelectedIcon()) {

                    Timer timer;
                    timer = new Timer(500, new ActionListener() {
                        public void actionPerformed(ActionEvent ae) {

                            letUnclicked();
                        }
                    });
                    timer.setRepeats(false);
                    timer.start();
                }
            } else if (countClicked() > 2) {
                btn.setSelected(false);
            }

        }
    }

} //end of code

1 个答案:

答案 0 :(得分:0)

显然,我的错误是比较 klicked [0] .getSelectedIcon()== klicked [1] .getSelectedIcon()

在eclipse调试器中我发现这些不相等,所以我将其改为

点击[0] .getSelectedIcon()。的toString()。等于(点击[1] .getSelectedIcon()。的toString())

现在,当我得到一场比赛时它会起作用。