修复:Java内存游戏 - Java不会显示其他图像

时间:2017-03-10 04:31:09

标签: java

更新:我只是手动编码每个按钮。谢谢,无论如何。

我正在尝试用Java编写一个记忆游戏。出于某种原因,Java将所有图像渲染为相同。它似乎在点击时将最新图像渲染到所有按钮。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.ArrayList;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;

/**
 * @author Steven
 *
 */
public class Memory extends JFrame {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    private static final int GRIDSIZE = 4;
    private PicButton[][] liteBut = new PicButton[GRIDSIZE][GRIDSIZE];
    private Random rand = new Random();
    private ClassLoader cl = this.getClass().getClassLoader();
    private String[] imagelist = {"images/image01.jpg", "images/image02.jpg", "images/image03.jpg", "images/image04.jpg"};
    private ArrayList<String> images = new ArrayList<String>();
    private volatile String icon = "";

    public Memory() {
        initGUI();
        setTitle("Memory");
        setResizable(false);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private void initGUI() {
        assignimages();
        TitleLabel framedTitle = new TitleLabel("Memory");
        add(framedTitle, BorderLayout.PAGE_START);

        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new GridLayout(GRIDSIZE, GRIDSIZE));
        add(centerPanel, BorderLayout.CENTER);

        for (int row = 0; row < GRIDSIZE; row++) {
            for (int col = 0; col < GRIDSIZE; col++) {
                liteBut[row][col] = new PicButton(row, col);

                if (row == 0) {
                    if (col == 0) {
                        icon = images.get(0);
                        System.out.println(icon);
                    } else if (col == 1) {
                        icon = images.get(1);
                        System.out.println(icon);
                    } else if (col == 2) {
                        icon = images.get(2);
                        System.out.println(icon);
                    } else if (col == 3) {
                        icon = images.get(3);
                        System.out.println(icon);
                    }

                } else if (row == 1) {
                    if (col == 0) {
                        icon = images.get(0);
                        System.out.println(icon);
                    } else if (col == 1) {
                        icon = images.get(1);
                        System.out.println(icon);
                    } else if (col == 2) {
                        icon = images.get(2);
                        System.out.println(icon);
                    } else if (col == 3) {
                        icon = images.get(3);
                        System.out.println(icon);
                    }

                }

                liteBut[row][col].addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        PicButton button = (PicButton) e.getSource();
                        int row = button.getRow();
                        int col = button.getCol();
                        String id = button.getID();
                        System.out.println("Hi from " + id);
                        liteBut[row][col].setIcon(new ImageIcon(cl.getResource(icon)));
                    }
                });
                centerPanel.add(liteBut[row][col]);
            }
        }

    }

    private void assignimages() {
        for (int x = 0; x < 4; x++) {
            int i = rand.nextInt(GRIDSIZE);
            images.add(imagelist[i]);
        }
        for (int x = 0; x < images.size(); x++) {
            System.out.println(images.get(x));
        }
    }

    public static void main(String[] args) {
        try {
            String className = UIManager.getCrossPlatformLookAndFeelClassName();
            UIManager.setLookAndFeel(className);
        } catch (Exception e) {
        }

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Memory();
            }
        });
    }

    public class PicButton extends JButton {

        private static final long serialVersionUID = 1L;
        private static final int MAXSIZE = 150;

        private int row = 0;
        private int col = 0;
        private String id = "";
        //private Boolean hasPic;

        public PicButton(int row, int col) {
            this.row = row;
            this.col = col;
            id = Integer.toString(row) + Integer.toString(col);
            System.out.println(id);
            setBackground(Color.BLACK);
            Dimension size = new Dimension(MAXSIZE, MAXSIZE);
            setPreferredSize(size);

        }

        public int getRow() {
            return row;
        }

        public int getCol() {
            return col;
        }

        public String getID() {
            return id;
        }

        public void setImage() {
            setBackground(Color.RED);
            //hasPic = true;
        }

        public void clearImage() {
            setBackground(Color.BLACK);
            //hasPic = false;
        }
    }

    public class TitleLabel extends JLabel {

        private static final long serialVersionUID = 1L;

        public TitleLabel(String title) {
            Font titleFont = new Font(Font.SERIF, Font.BOLD, 32);
            setFont(titleFont);
            setHorizontalAlignment(JLabel.CENTER);
            setText(title);
            setBackground(Color.BLACK);
            setForeground(Color.WHITE);
            setOpaque(true);
        }
    }

}

1 个答案:

答案 0 :(得分:1)

liteBut[row][col].setIcon(new ImageIcon(cl.getResource(icon)));

将最后已知值icon作为图像分配给按钮(这是一个实例字段,因此它会记住),这不是&#34;值&#34;已分配给该按钮,但是您创建按钮的icon分配给for-loop的最后一个值,所以基本上,所有按钮都会获得最后一个icon,而是提供图标值PictureButton本身,因此您可以更新&#34;单击按钮时的按钮。

此功能可以自包含在按钮本身,进一步使管理更容易