簡體   English   中英

線程“ main”中的異常java.lang.ArrayIndexOutOfBoundsException:8

[英]Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 8

我已經瀏覽了其他相同類型的問題,並試圖找到答案,但是我看不到有什么問題,如果有人可以指出對我來說太好了!

因此,基本上,我正在創建一個“記憶游戲”,其中我必須為每張卡設置默認的背景圖像,但是每次我再次運行它時,該圖像都必須更改。 我也要用前景圖像(21張)創建和排列列表,但每場比賽我只用8張。 每當我運行該程序時,它們也必須更改。 也就是說,到目前為止,我得到的是:

public class GameBoard extends javax.swing.JPanel implements Runnable {

    private JLabel[] _Labels = new JLabel[8];
    private Card[] _Cards = new Card[16];

    public GameBoard(){

        setLayout(new GridLayout(4, 4));

        for(int i = 0; i<8; i=i+1){

            _Labels[i] = new JLabel();

        }

        for(int i = 0; i<16; i=i+2){

            _Cards[i] = new Card(_Labels[i]);
            _Cards[i+1] = new Card(_Labels[i]);

            add(_Cards[i].getLabel());
            add(_Cards[i+1].getLabel());

            _Labels[i].addMouseListener(new LabelListener(_Cards[i]));
            _Labels[i+1].addMouseListener(new LabelListener(_Cards[i+1]));

        }

    }

    @Override
    public void run() {


    }

}

卡類為:

public class Card{

    private JLabel _Label;

    ImageIcon imageOne;

    ImageIcon imageTwo;

    public Card(JLabel once){

        _Label = once;

 //-----------------------Foreground--------------------------------

        ArrayList<String> foregroundCard = new ArrayList<String>();       
        int i = 1;
        while(i < 23){
            if(i > 9){
                String addGreaterNine = "F" + i + ".png";
                foregroundCard.add(addGreaterNine);
            }
            else{
                String addLesserNine = "F0" + i +".png";
                foregroundCard.add(addLesserNine);
            }
            i = i + 1;
        }

        //-----------------Add 8---------------------

        int a = 0;

        while(a < 8){

            Collections.shuffle(foregroundCard); 

            ArrayList<String> stringsGoingToBeUsed = new ArrayList<String>();            
            stringsGoingToBeUsed.add(foregroundCard.get(0));
            stringsGoingToBeUsed.add(foregroundCard.get(0));

            for(int l = 0; l<16; l=l+1){

            Collections.shuffle(stringsGoingToBeUsed);  

            String frontName = "Images/" + stringsGoingToBeUsed.get(0);
            imageOne = new ImageIcon(frontName);

            }

            a = a + 1;

        }       


//------------------------Background--------------------------------        

        ArrayList<String> backgroundCard = new ArrayList<String>();
        backgroundCard.add("B01.png");
        backgroundCard.add("B02.png");
        backgroundCard.add("B03.png");
        Collections.shuffle(backgroundCard);
        String location = "Images/" + backgroundCard.get(0);
        imageTwo = new ImageIcon(location);

//-------------------------------------------------------------------        

        _Label.setIcon(imageTwo);   

    }
    public void TurnCard (){

        ImageIcon temp;
        temp = imageTwo;
        imageTwo = imageOne;
        imageOne = temp;
        _Label.setIcon(imageTwo);

    }

    public JLabel getLabel(){

        return _Label;

    }

}

LabelListener類是:

public class LabelListener implements MouseListener {

    private Card _Card;

    public LabelListener(Card c){

        _Card = c;

    }

    @Override
    public void mouseClicked(MouseEvent arg0) {

        _Card.TurnCard();

    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

}

主要方法是:

public class Game {
    public static void main(String[] args) {

        JFrame main = new JFrame("Memory Game");
        main.add(new GameBoard());  

        main.pack();
        main.setVisible(true);
        main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}

我懷疑是因為_Labels是一個包含8個元素的數組,但是您嘗試在以下代碼中最多引用16個:

for(int i = 0; i<16; i=i+2){
    ...
    _Labels[i+1].addMouseListener(new LabelListener(_Cards[i+1]));
    ...
for(int i = 0; i<16; i=i+2){

        _Cards[i] = new Card(_Labels[i]);
        _Cards[i+1] = new Card(_Labels[i]);

        add(_Cards[i].getLabel());
        add(_Cards[i+1].getLabel());

        _Labels[i].addMouseListener(new LabelListener(_Cards[i]));
        _Labels[i+1].addMouseListener(new LabelListener(_Cards[i+1]));

    }

不能使用_Labels大小僅為8的方法,可能的解決方案是將其拆分為單獨的循環或更改程序體系結構。

由於CardJLabel數組項的數量不匹配,這會導致ArrayIndexOutOfBoundsException 您可以為此添加另一個索引j

for (int i = 0, j = 0; i < 16; i += 2, j++) {

   _Cards[i] = new Card(_Labels[j]); // AIOOBE was happening here
   _Cards[i + 1] = new Card(_Labels[j]);

   add(_Cards[i].getLabel());
   add(_Cards[i + 1].getLabel());

   _Labels[j].addMouseListener(new LabelListener(_Cards[i]));

   // not necessary
   // _Labels[j + 1].addMouseListener(new LabelListener(_Cards[i + 1])); 
}

另外:使用Java命名約定來命名變量,例如使用cards代替_Cards

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM