使用JButtons创建记忆游戏

时间:2017-11-30 16:03:18

标签: java swing jbutton

问题

我无法找到一个好的算法,只允许我在切换的图像上同时拥有两个选定的按钮。如果两个按钮上的切换图像相同,我想隐藏它们。如果不是,我希望他们回到原来的形象,我在卡面朝下时使用(笑脸)。我使用一个在JFrame中打开我的JPanel的启动类。 我不是母语人士,所以我希望自己能说清楚。

代码

public class MemoryMain extends JPanel implements ActionListener{

    private ImageIcon iconStart = new ImageIcon ("images\\smiley.jpg");
    private ImageIcon icon1 = new ImageIcon ("images\\amazed.jpg");
    private ImageIcon icon2 = new ImageIcon ("images\\datass.jpg");
    private ImageIcon icon3 = new ImageIcon ("images\\lol.jpg");
    private ImageIcon icon4 = new ImageIcon ("images\\megusta.jpg");
    private ImageIcon icon5 = new ImageIcon ("images\\mog.png");
    private ImageIcon icon6 = new ImageIcon ("images\\satisfied.jpg");
    private JButton btn1 = new JButton ();
    private JButton btn2 = new JButton ();
    private JButton btn3 = new JButton ();
    private JButton btn4 = new JButton ();
    private JButton btn5 = new JButton ();
    private JButton btn6 = new JButton ();
    private JButton btn7 = new JButton ();
    private JButton btn8 = new JButton ();
    private JButton btn9 = new JButton ();
    private JButton btn10 = new JButton ();
    private JButton btn11 = new JButton ();
    private JButton btn12 = new JButton ();
    private ImageIcon[] iconArray = new ImageIcon[12];

    public MemoryMain () {
        setPreferredSize(new Dimension (800, 600));
        setBackground (Color.CYAN);
        this.setLayout(new GridLayout(3, 4));

        btn1.setIcon(iconStart);
        btn2.setIcon(iconStart);
        btn3.setIcon(iconStart);
        btn4.setIcon(iconStart);
        btn5.setIcon(iconStart);
        btn6.setIcon(iconStart);
        btn7.setIcon(iconStart);
        btn8.setIcon(iconStart);
        btn9.setIcon(iconStart);
        btn10.setIcon(iconStart);
        btn11.setIcon(iconStart);
        btn12.setIcon(iconStart);

        btn1.setPreferredSize(new Dimension (80,80));
        btn2.setPreferredSize(new Dimension (80,80));
        btn3.setPreferredSize(new Dimension (80,80));
        btn4.setPreferredSize(new Dimension (80,80));
        btn5.setPreferredSize(new Dimension (80,80));
        btn6.setPreferredSize(new Dimension (80,80));
        btn7.setPreferredSize(new Dimension (80,80));
        btn8.setPreferredSize(new Dimension (80,80));
        btn9.setPreferredSize(new Dimension (80,80));
        btn10.setPreferredSize(new Dimension (80,80));
        btn11.setPreferredSize(new Dimension (80,80));
        btn12.setPreferredSize(new Dimension (80,80));

        btn1.addActionListener(this);
        btn2.addActionListener(this);
        btn3.addActionListener(this);
        btn4.addActionListener(this);
        btn5.addActionListener(this);
        btn6.addActionListener(this);
        btn7.addActionListener(this);
        btn8.addActionListener(this);
        btn9.addActionListener(this);
        btn10.addActionListener(this);
        btn11.addActionListener(this);
        btn12.addActionListener(this);

        add(btn1);
        add(btn2);
        add(btn3);
        add(btn4);
        add(btn5);
        add(btn6);
        add(btn7);
        add(btn8);
        add(btn9);
        add(btn10);
        add(btn11);
        add(btn12);
        this.iconArray = randomArray();

    }
    public void actionPerformed(ActionEvent e) {


        if (e.getSource() == btn1) {
            Image temp = iconArray[0].getImage();
            Image resizedImage = temp.getScaledInstance(200, 200,  java.awt.Image.SCALE_SMOOTH);
            ImageIcon icon = new ImageIcon(resizedImage);
            btn1.setIcon(icon);

        }
        if (e.getSource() == btn2) {
            Image temp = iconArray[1].getImage();
            Image resizedImage = temp.getScaledInstance(200, 200,  java.awt.Image.SCALE_SMOOTH);
            ImageIcon icon = new ImageIcon(resizedImage);
            btn2.setIcon(icon);

        }
        if (e.getSource() == btn3) {
            Image temp = iconArray[2].getImage();
            Image resizedImage = temp.getScaledInstance(200, 200,  java.awt.Image.SCALE_SMOOTH);
            ImageIcon icon = new ImageIcon(resizedImage);
            btn3.setIcon(icon);
        }
        if (e.getSource() == btn4) {
            Image temp = iconArray[3].getImage();
            Image resizedImage = temp.getScaledInstance(200, 200,  java.awt.Image.SCALE_SMOOTH);
            ImageIcon icon = new ImageIcon(resizedImage);
            btn4.setIcon(icon);
        }
        if (e.getSource() == btn5) {
            Image temp = iconArray[4].getImage();
            Image resizedImage = temp.getScaledInstance(200, 200,  java.awt.Image.SCALE_SMOOTH);
            ImageIcon icon = new ImageIcon(resizedImage);
            btn5.setIcon(icon);
        }
        if (e.getSource() == btn6) {
            Image temp = iconArray[5].getImage();
            Image resizedImage = temp.getScaledInstance(200, 200,  java.awt.Image.SCALE_SMOOTH);
            ImageIcon icon = new ImageIcon(resizedImage);
            btn6.setIcon(icon);
        }
        if (e.getSource() == btn7) {
            Image temp = iconArray[6].getImage();
            Image resizedImage = temp.getScaledInstance(200, 200,  java.awt.Image.SCALE_SMOOTH);
            ImageIcon icon = new ImageIcon(resizedImage);
            btn7.setIcon(icon);
        }
        if (e.getSource() == btn8) {
            Image temp = iconArray[7].getImage();
            Image resizedImage = temp.getScaledInstance(200, 200,  java.awt.Image.SCALE_SMOOTH);
            ImageIcon icon = new ImageIcon(resizedImage);
            btn8.setIcon(icon);
        }
        if (e.getSource() == btn9) {
            Image temp = iconArray[8].getImage();
            Image resizedImage = temp.getScaledInstance(200, 200,  java.awt.Image.SCALE_SMOOTH);
            ImageIcon icon = new ImageIcon(resizedImage);
            btn9.setIcon(icon);
        }
        if (e.getSource() == btn10) {
            Image temp = iconArray[9].getImage();
            Image resizedImage = temp.getScaledInstance(200, 200,  java.awt.Image.SCALE_SMOOTH);
            ImageIcon icon = new ImageIcon(resizedImage);
            btn10.setIcon(icon);
        }
        if (e.getSource() == btn11) {
            Image temp = iconArray[10].getImage();
            Image resizedImage = temp.getScaledInstance(200, 200,  java.awt.Image.SCALE_SMOOTH);
            ImageIcon icon = new ImageIcon(resizedImage);
            btn11.setIcon(icon);
        }
        if (e.getSource() == btn12) {
            Image temp = iconArray[11].getImage();
            Image resizedImage = temp.getScaledInstance(200, 200,  java.awt.Image.SCALE_SMOOTH);
            ImageIcon icon = new ImageIcon(resizedImage);
            btn12.setIcon(icon);

        }

    }
    public ImageIcon[] randomArray () {
        iconArray[0] = icon1;
        iconArray[1] = icon1;
        iconArray[2] = icon2;
        iconArray[3] = icon2;
        iconArray[4] = icon3;
        iconArray[5] = icon3;
        iconArray[6] = icon4;
        iconArray[7] = icon4;
        iconArray[8] = icon5;
        iconArray[9] = icon5;
        iconArray[10] = icon6;
        iconArray[11] = icon6;

        for (int i = iconArray.length - 1; i > 0; i--) {
            int j = (int) Math.floor(Math.random() * (i + 1));
            ImageIcon temp = iconArray[i];
            iconArray[i] = iconArray[j];
            iconArray[j] = temp;
        }
        return iconArray;
    }
}

1 个答案:

答案 0 :(得分:0)

主要问题是您不使用数组。您正在处理按钮的12个btn__字段以及图标的6个icon_字段。当您编写actionPerformed()方法时,您应该意识到“必须有一种比检查btn1btn12”更简单的方法。答案是“数组”。

将所有图标保存在数组中,以便您轻松使用它们。保存数组中的所有按钮,以便您也可以轻松使用它们。拥有此功能后,您可以使用简单的数组索引访问来访问正确的图标或按钮。也许你可以使用一个额外的阵列来保存卡面临或面临的信息。或者甚至可以创建一个新的类Card,其面向上时图标的字段为picture,如果此卡当前面朝上或朝下,则保存一个布尔字段isFacedUp