线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:8

时间:2013-04-23 02:25:20

标签: java compiler-errors

我已经查看了同类型的其他问题并试图找到答案,但我无法看到有什么问题,如果有人可以为我指出它会很棒!

所以基本上我正在创建一个“记忆游戏”,我必须为每张卡设置一个默认的背景图像,但每次重新运行时这个图像都必须改变。此外,我必须使用前景图像(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);

    }
}

3 个答案:

答案 0 :(得分:2)

我怀疑是因为_Labels是一个包含8个元素的数组,但是您尝试在以下代码中引用最多16个:

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

答案 1 :(得分:0)

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

    }

无法工作,因为_Label只有8号,可能的解决方案是将其拆分为单独的循环或更改程序架构。

答案 2 :(得分:0)

由于产生Card的{​​{1}}和JLabel数组项的数量不匹配。您可以添加另一个索引ArrayIndexOutOfBoundsException来解释此问题:

j

除此之外:使用Java命名约定命名变量,例如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])); } 代替cards