为什么我的动作侦听器在单击时没有显示正确的图像?

Why is my action-listener not displaying the proper image when clicked?

我目前正在尝试编写配对游戏的代码。我为 JButtonImageIcon 创建了一个二维数组。我已经对 class 名称 Shuffle 进行了改组,此 class 改组了 ImageIcon 在其数组中的位置。在没有基础牌的情况下开始游戏时,牌似乎被洗牌了。但是当我用基本卡片开始游戏并合并动作侦听器时,当你点击卡片时没有图像。如果有人能帮我解决这个问题,我将不胜感激。

考虑这段代码:

框架方法:

void game() {
        Shuffle shuffle = new Shuffle();
        shuffle.random2();
        ImageIcon base = new ImageIcon("images/BaseCard.png");
        int x = 60;
        int y = 20;
        JFrame frame = obj.frame();
        JLabel label = obj.label();
        for (int i = 0; i < cards.length; ++i) {
            for (int j = 0; j < cards[i].length; ++j) {
                cards[i][j] = obj.Comp(base);
                cards[i][j].addActionListener(new Clicked(i, j));
                cards[i][j].setBounds(x, y, 90, 126);
                y = y + 135;
                if (y >= 540) {
                    y = 20;
                    x = x + 120;
                }
                frame.add(cards[i][j]);
            }
        }

        frame.add(label);
        frame.setLayout(null);
        frame.setVisible(true);
    }
}

这是随机播放 class:

class Shuffle {

    Matching obj = new Matching();
    String peach = "images/peach.png";
    String daisy = "images/Baby_daisy.png";
    String luigi = "images/Baby_Luigi.png";
    String waluigi = "images/Baby_Waluigi.png";
    String wario = "images/Baby_Wario.png";
    String bowser = "images/BabyBowser.png";
    String drybones = "images/DryBones.png";
    String shyguy = "images/ShyGuy.png";

    String[][] images = {
        {peach, daisy, luigi, waluigi},
        {wario, bowser, drybones, shyguy},
        {peach, daisy, luigi, waluigi},
        {wario, bowser, drybones, shyguy}
    };

    ImageIcon Icons[][] = new ImageIcon[4][4];

    void random2() {

        for (int i = 0; i < images.length; i++) {
            for (int j = 0; j < images[i].length; j++) {
                int i1 = (int) (Math.random() * images.length);
                int j1 = (int) (Math.random() * images[i].length);

                String temp = images[i][j];
                images[i][j] = images[i1][j1];
                images[i1][j1] = temp;
            }
        }

        for (int i = 0; i < images.length; i++) {
            for (int j = 0; j < images[i].length; j++) {
                Icons[i][j] = new ImageIcon(images[i][j]);
            }
        }

    }
}

这是动作监听器

class Clicked implements ActionListener {

    Shuffle shuffle = new Shuffle();
    Matching matching = new Matching();
    private int i;
    private int j;

    public Clicked(int i, int j) {
        this.i = i;
        this.j = j;

    }

    public void actionPerformed(ActionEvent e) {
        JToggleButton tBtn = (JToggleButton) e.getSource();
        if (tBtn.isSelected()) {
            System.out.println("click");
            tBtn.setIcon(shuffle.Icons[i][j]);
        } else {
            System.out.println("not");
            tBtn.setIcon(new ImageIcon("images/BaseCard.png"));
        }

    }

}

在您的 Clicked class 中,您正在创建一个新的 Shuffle 而不是:

public Clicked(Shuffle shuffle) {
    this.shuffle = shuffle;
}

这会给你之前创建和洗好的 "deck" 张牌。您可能还想研究只使用 Clicked 并将其用于所有卡片,如果您开始更改 Shuffle.

,请注意任何线程问题