简体   繁体   中英

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

i have created a matching card game with JButtons for my Gr12 assignment just for extra marks but i am having trouble with changing the second card icon before removing the button or changing the icon to null. I have found the code:

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

And this is my 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++;

        }
    }
    ;

Here is where i set each button to the 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);

Now the problem i am having is that whenever two cards do or don't match 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... Please forgive my sloppy code this is my first time using actionListeners and JButtons and i am still only in Gr12. Any help is great.

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);

Don't use Thread.sleep(). The will cause the Event Dispatch Thread (EDT) to sleep, which means the GUI can't repaint itself. Read the section from the Swing tutorial on Concurrency for more information about the EDT.

Instead you will need to use a Swing Timer to schedule the event. So you will likely need to restructure your code to take advantage of the Timer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM