[英]Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 8
I've looked through some other question of the same type and trying to find the answer but I wasn't able to see what is wrong, if someone could point it out for me would be great! 我已经浏览了其他相同类型的问题,并试图找到答案,但是我看不到有什么问题,如果有人可以指出对我来说太好了!
So basically I'm creating a "Memory Game" in which I have to set up a default background image to each card, but this image has to change each time I run it again. 因此,基本上,我正在创建一个“记忆游戏”,其中我必须为每张卡设置默认的背景图像,但是每次我再次运行它时,该图像都必须更改。 Also I gotta create and array list with the foreground images (which are 21) but I'll only use 8 per game. 我也要用前景图像(21张)创建和排列列表,但每场比赛我只用8张。 They also have to change each time I run the program. 每当我运行该程序时,它们也必须更改。 That said, what I got so far is: 也就是说,到目前为止,我得到的是:
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() {
}
}
The card class is: 卡类为:
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;
}
}
And the LabelListener class is: 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
}
}
The main method is: 主要方法是:
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);
}
}
I suspect it's because _Labels
is an array with 8 elements, however you're attempting to reference up to 16 in the following code: 我怀疑是因为_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]));
}
Doesnt work as _Labels is only size 8 a possible solution would be to split this into separate loops or change the program architecture. 不能使用_Labels大小仅为8的方法,可能的解决方案是将其拆分为单独的循环或更改程序体系结构。
As there is a mismatch between the number of Card
and JLabel
array items which produces the ArrayIndexOutOfBoundsException
. 由于Card
和JLabel
数组项的数量不匹配,这会导致ArrayIndexOutOfBoundsException
。 You could add another index j
to account for this: 您可以为此添加另一个索引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]));
}
Aside: Use Java naming conventions for naming variables, such as cards
in place of _Cards
. 另外:使用Java命名约定来命名变量,例如使用cards
代替_Cards
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.