繁体   English   中英

Java中的Matching Card Game,JButton在删除图标或将Icon更改为null之前不会显示Icon

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

我已经为我的Gr12任务创建了一个与JButtons匹配的纸牌游戏,只是为了获得额外的分数,但是在删除按钮或将图标更改为null之前,我无法更改第二张纸牌图标。 我找到了代码:

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中。 任何帮助都很棒。

而不是显示第二张卡,然后延迟然后移除两张卡或仅移除图标...延迟将按钮保持在按下位置,而从不显示第二张图标

Thread.sleep (1000);

不要使用Thread.sleep()。 这将导致Event Dispatch Thread (EDT)进入睡眠状态,这意味着GUI无法重绘自身。 阅读Swing 并发教程中有关EDT的更多信息。

相反,您将需要使用Swing计时器来安排事件。 因此,您可能需要重组代码以利用Timer。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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