匹配纸牌游戏,在 Java 中,JButton 在删除图标或将图标更改为 null 之前不会显示图标

Matching Card Game, in Java The JButton won't show the Icon before removing it or changing Icon to null

我已经用 JButtons 为我的 Gr12 作业创建了一个匹配的纸牌游戏,只是为了获得额外的分数,但我在移除按钮或将图标更改为空之前更改第二张纸牌图标时遇到了问题。我找到了代码:

parent [0].remove (button [0]);
parent [0].repaint ();
parent [1].remove (button [1]);
parent [1].repaint ();

这是我的 actionListener:

ActionListener actionListener = new ActionListener ()
    {
        int nomore = 0;
        int total = 16;
        int nums[] [] = new int [4] [4];
        int number[] = new int [2];
        JButton[] button = new JButton [2];
        Container parent[] = new Container [2];
        int tries = 0;
        String label[] = new String [2];

        public void actionPerformed (ActionEvent actionEvent)
        {
            if (nomore == 0)
            {

                int i, j, x, num, times;
                for (j = 0, num = 1, times = 0 ; j < 4 ; j++)
                {
                    for (i = 0 ; i < 4 ; i++, times++)
                    {
                        nums [i] [j] = num;
                        if (times == 1)
                        {
                            num++;
                            times = -1;
                        }
                    }
                }
                int tempnum;
                j = 0;
                i = 0;
                int r = 0, t = 0;
                for (int count = 0 ; count < total ; count++)
                {
                    j = (int) (Math.random () * 4);
                    i = (int) (Math.random () * 4);
                    r = (int) (Math.random () * 4);
                    t = (int) (Math.random () * 4);
                    tempnum = nums [i] [j];
                    nums [i] [j] = nums [r] [t];
                    nums [r] [t] = tempnum;
                }

                ImageIcon image[] = new ImageIcon [total];
                /*for (j = 0, num = 1, times = 0 ; j < 4 ; j++)
                {
                    for (i = 0 ; i < 4 ; i++)
                    {
                        c.println (nums [i] [j]);

                    }

                }*/
            }
            //System.out.println (actionEvent.getSource ());
            button [tries] = (JButton) actionEvent.getSource ();

            label [tries] = button [tries].getLabel ();

            int x = ((int) label [tries].charAt (0)) - 65;
            int y = ((int) label [tries].charAt (1)) - 65;
            c.println (x);
            c.println (y);


            parent [tries] = button [tries].getParent ();


            number [tries] = nums [x] [y] - 1;

            ImageIcon image = new ImageIcon (getClass ().getResource ("Flag" + number [tries] + ".jpg "));


            button [tries].setIcon (image);

            //button.setText("Hello");

            /*Container parent = button.getParent ();
            parent.remove (button);
            parent.repaint ();*/


            //String label2 = button.getText ();
            // System.out.println (label2);
            nomore = 1;

            if (tries == 1)
            {
                //slow
                try
                {
                    Thread.sleep (1000);
                }
                catch (InterruptedException ex)
                {
                    Thread.currentThread ().interrupt ();
                }
                //slow ends
                if (number [0] == number [1] && label [0] != label [1])
                {
                    parent [0].remove (button [0]);
                    parent [0].repaint ();
                    parent [1].remove (button [1]);
                    parent [1].repaint ();
                }
                else if (number [0] != number [1] && label [0] != label [1])
                {

                    button [0].setIcon (null);


                    button [1].setIcon (null);
                }

                tries = -1;
            }
            tries++;

        }
    }
    ;

这里是我将每个按钮设置为 actionListener 的地方:

for (int j = 0, ilet1 = 65 ; j < y1 ; j++, ilet1++)
    {
        for (int i = 0, ilet2 = 65 ; i < x1 ; i++, ilet2++)
        {
            char clet1 = (char) ilet1;
            char clet2 = (char) ilet2;
            String slet = "" + clet2 + clet1;
            //ImageIcon image12 = new ImageIcon (("Flag12.jpg"));
            //button [i] [j] = new JButton (image12);
            button [i] [j] = new JButton (slet);
            button [i] [j].addActionListener (actionListener);

            button [i] [j].setBounds
                (sizex * i + 10 * i + 60, sizey * j + 10 * j + 60, sizex, sizey);

            frame.getContentPane ().add (button [i] [j]);



        }
    }
    frame.setVisible (true);

现在我遇到的问题是,每当两张卡片匹配或不匹配时,而不是显示第二张卡片,然后延迟,然后移除两张卡片或只是移除图标...延迟将按钮保持在按下位置并且从不显示第二个图标...请原谅我草率的代码这是我第一次使用 actionListeners 和 JButtons,我仍然只在 Gr12。任何帮助都很棒。

instead of showing the second card then a delay and then removing both cards or just removing the icon... the delay holds the button in the pressed down position and never shows the second icon

Thread.sleep (1000);

不要使用 Thread.sleep()。这将导致 Event Dispatch Thread (EDT) 休眠,这意味着 GUI 无法自行重绘。阅读有关 Concurrency 的 Swing 教程部分,了解有关 EDT 的更多信息。

相反,您需要使用 Swing Timer 来安排活动。因此,您可能需要重构代码以利用定时器。