簡體   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