Java 记忆游戏:一些问题

Java 记忆游戏:一些问题,java,arrays,jbutton,Java,Arrays,Jbutton,我试图创建一个图像记忆游戏。用户必须匹配图片 我现在有两个问题: 我不明白如何在开始时隐藏图片(而不是现在就让它们可见) 我还想弄清楚如何比较这些图片 这是密码 多谢各位 package lib; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MemoryGame extends JFrame implements ActionListener { private Ti

我试图创建一个图像记忆游戏。用户必须匹配图片

我现在有两个问题:

  • 我不明白如何在开始时隐藏图片(而不是现在就让它们可见)
  • 我还想弄清楚如何比较这些图片
  • 这是密码

    多谢各位

    package lib;
    
    import javax.swing.*;
    
    import java.awt.*;
    import java.awt.event.*;
    
    public class MemoryGame extends JFrame implements ActionListener {
    
        private Timer tmrTime;
        private int seconds = 0;
    
        private JPanel panel;
        private JLabel lblTimer;
    
        private JButton btnStart;
        private int p, q, c, count = 0;
        private int[][] check = new int[4][4];
    
        private static ImageIcon one = new ImageIcon("a.png"), two = new ImageIcon(
                "b.png"), three = new ImageIcon("c.png"), four = new ImageIcon(
                "d.png"), five = new ImageIcon("e.png"), six = new ImageIcon(
                "f.png"), seven = new ImageIcon("g.png"), eight = new ImageIcon(
                "h.png");
    
        private static ImageIcon[] a = { one, two, three, four, five, six, seven,
                eight };
        private static JButton[][] b = new JButton[4][4];
    
        public static void main(String[] args) {
    
            new MemoryGame();
        }
    
        public MemoryGame() {
    
            for (int i = 0; i < a.length; i++) {
                c = 0;
                do {
                    p = (int) (Math.random() * 4);
                    q = (int) (Math.random() * 4);
                    if (check[p][q] == 0) {
                        c++;
                        check[p][q]++;
                        b[p][q] = new JButton(a[i]);
                        b[p][q].setPreferredSize(new Dimension(150, 100));
    
    
                        b[p][q].setBorder(BorderFactory.createLineBorder(Color.black));
                        b[p][q].setMargin(new Insets(0, 0, 0, 0));
                        //b[p][q].setBorder(BorderFactory.createEmptyBorder());
                        b[p][q].setContentAreaFilled(false);
                        b[p][q].addActionListener(this);
    
    
                    }
                } while (c != 2);
            }
    
            panel = new JPanel();
            panel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));
    
            JLabel lblTitle = new JLabel("MEMORY GAME");
            lblTitle.setPreferredSize(new Dimension(700, 30));
            lblTitle.setFont(new Font("Britannic Bold", Font.BOLD, 28));
            lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
            panel.add(lblTitle);
    
    
    
            JLabel lblTime = new JLabel("ELAPSED TIME:");
            lblTime.setFont(new Font("Britannic Bold", Font.BOLD, 18));
    
            lblTimer = new JLabel();
            lblTimer.setPreferredSize(new Dimension(100, 20));
            lblTimer.setFont(new Font("Britannic Bold", Font.BOLD, 18));
            lblTimer.setHorizontalAlignment(SwingConstants.CENTER);
    
            btnStart = new JButton("START");
            btnStart.setPreferredSize(new Dimension(660, 30));
            btnStart.setFocusable(false);
            btnStart.setFont(new Font("Tahoma", Font.BOLD, 14));
            btnStart.addActionListener(this);
    
    
    
            for (int i = 0; i < b.length; i++) {
                for (int j = 0; j < b[i].length; j++) {
                    panel.add(b[i][j]);
                }
            }
    
            panel.add(btnStart);
            panel.add(lblTime);
            panel.add(lblTimer);
    
            setContentPane(panel);
            setSize(800, 720);
            setTitle("Memory Game");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationRelativeTo(null);
            setVisible(true);
        }
    
        public void actionPerformed(ActionEvent e) {
    
            seconds++;
            int minutes = seconds / 60;
            int sec = seconds % 60;
    
    
            if (sec < 10) {
                lblTimer.setText("" + minutes + ":0" + sec);
            } else if (seconds > 9) {
                lblTimer.setText("" + minutes + ":" + sec);
            }
    
            if (e.getSource() == btnStart && count == 0) {
                btnStart.setText("EXIT");
                for (int i = 0; i < b.length; i++) {
                    for (int j = 0; j < b[i].length; j++) {
    
                        //b[i][j].setBorder(BorderFactory.createLineBorder(Color.black));
                        b[i][j].setOpaque(false);
                        b[i][j].setContentAreaFilled(false);
                        b[i][j].setBorderPainted(false);
    
    
                    }
                }
    
                count++;
    
                tmrTime = new Timer(1000, this);
                tmrTime.start();
    
    
            } else if (e.getSource() == btnStart && count > 0) {
                int option = JOptionPane.showConfirmDialog(null,
                        "Are you sure you want to exit ?", "Memory Game",
                        JOptionPane.YES_NO_OPTION);
                if (option == JOptionPane.YES_OPTION) {
                    System.exit(0);
                }
            } else   { 
    
    
                if (e.getSource() == b[p][q]) { 
                    b[p][q].getSource();
                } 
            }
    
    
    
        }
    }
    
    包库;
    导入javax.swing.*;
    导入java.awt.*;
    导入java.awt.event.*;
    公共类MemoryGame扩展JFrame实现ActionListener{
    私人定时器时间;
    私有整数秒=0;
    私人JPanel小组;
    私人JLabel-lblTimer;
    私人JButton btnStart;
    私有整数p,q,c,计数=0;
    私有整数[][]检查=新整数[4][4];
    私有静态图像图标一=新图像图标(“a.png”),二=新图像图标(
    “b.png”),三个=新图像图标(“c.png”),四个=新图像图标(
    “d.png”),五个=新图像图标(“e.png”),六个=新图像图标(
    “f.png”),七个=新图像图标(“g.png”),八个=新图像图标(
    “h.png”);
    私有静态图像图标[]a={1,2,3,4,5,6,7,
    八};
    私有静态JButton[][]b=新JButton[4][4];
    公共静态void main(字符串[]args){
    新记忆名();
    }
    公共记忆名称(){
    for(int i=0;i9){
    lblTimer.setText(“+minutes+”:“+sec”);
    }
    如果(e.getSource()==btnStart&&count==0){
    btnStart.setText(“退出”);
    for(int i=0;i0),则为else{
    int option=JOptionPane.showConfirmDialog(null,
    “你确定要退出吗?”,“记忆游戏”,
    JOptionPane.YES\u NO\u选项);
    if(option==JOptionPane.YES\u选项){
    系统出口(0);
    }
    }否则{
    如果(e.getSource()==b[p][q]){
    b[p][q].getSource();
    } 
    }
    }
    }
    
    我认为您应该在照片和一些唯一id之间映射。例如,每个图片都由uid表示,当您想要显示图片时,使用映射[uid]并比较图片,只需比较uid即可

    问题出在哪里?如果作业在这一点上对你来说有点太难(这是可以理解的——我们都有过),试着用更简单的程序练习,并逐步提高你的水平。遗憾的是,我们不是来为您分配任务的。我不是在寻求解决方案(这不是我来这里的原因),但类似示例的简单链接可以帮助我。1。为此,您可以将所有按钮的图标初始设置为某个空白或占位符图像,单击“开始”按钮后,您可以将按钮重新初始化为相应的图像。2.使用ShaZiv解释的地图