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

时间:2015-11-23 14:23:20

标签: java jbutton actionlistener imageicon

我已经创建了一个与JButtons相匹配的纸牌游戏,用于我的Gr12作业,只是为了额外的标记,但我在删除按钮或将图标更改为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和JButton,我仍然只在Gr12。任何帮助都很棒。

0 个答案:

没有答案