当前位置: 代码迷 >> Eclipse >> Exception in thread "main"java.lang.ArrayIndexOutofBoundsException,该如何处理
  详细解决方案

Exception in thread "main"java.lang.ArrayIndexOutofBoundsException,该如何处理

热度:63   发布时间:2016-04-23 13:29:31.0
Exception in thread "main"java.lang.ArrayIndexOutofBoundsException
这是我写的一个小拼图游戏 就是把0-8按顺序拼好 可是在DOS运行提示我14和34行Exception in thread "main"java.lang.ArrayIndexOutofBoundsException 求指点!不胜感激!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class SlidingPuzzle implements MouseListener{
  public static int level=3;
  public PuzzleTile[][] XueButtonArray= new PuzzleTile[level-1][level-1];
  JFrame Xueframe= new JFrame();
  JPanel Xuepanel= new JPanel();
  
  public static void main(String[] args){
SlidingPuzzle XueGame= new SlidingPuzzle();
XueGame.start();
}
 
public void start(){
Xueframe.setTitle("Numeric Slding Puzzle");
Container XueContainer=Xueframe.getContentPane();
XueContainer.setLayout(new GridLayout(level,level,1,1));

int[][] a=new int[level][level];
a[0][0]=(int)(Math.random()*(level*level));
this.XueButtonArray[0][0]=new PuzzleTile(a[0][0]);
this.XueButtonArray[0][0].setTileNumber(""+a[0][0]);
this.XueButtonArray[0][0].addMouseListener(this);
XueContainer.add(XueButtonArray[0][0]);
for(int i=0;i<level;i++){
for(int j=1;j<level;j++){
a[i][j]=(int)(Math.random()*(level*level));
do {
a[i][j]=(int)(Math.random()*(level*level));}while(a[i][j]==a[0][0]);
String tileNumber=""+a[i][j];
this.XueButtonArray[i][j]=new PuzzleTile(a[i][j]);
this.XueButtonArray[i][j].setTileNumber(tileNumber);
this.XueButtonArray[i][j].addMouseListener(this);
XueContainer.add(XueButtonArray[i][j]);
}
}
Xueframe.setSize(400, 400);
Xueframe.setResizable(false); // so that the frame size will not change
Xueframe.setVisible(true);
}

public void mouseEntered(MouseEvent e) {
PuzzleTile eButton = (PuzzleTile) e.getSource();
if (!eButton.getTileNumber().equals(""+0)) // change background color when mouse is over a tile
eButton.setBackground(Color.gray);
}

public void mousePressed(MouseEvent event) {
}
public void mouseReleased(MouseEvent event) {
}

public void mouseExited(MouseEvent e) {
PuzzleTile eButton = (PuzzleTile) e.getSource();
if (!eButton.getTileNumber().equals(""+0)) // change the color to its initial condition when mouse leave
eButton.setBackground(Color.darkGray);
}
public void mouseClicked(MouseEvent e) {
int[] XueNum=new int[4];
int i=0,j=0,n=0,m=0;
PuzzleTile eButton = (PuzzleTile)e.getSource(); // help to locate the button being clicked
if (eButton.getTileNumber().equals(""+0)) // see if it's the white color tile
; //nothing happens
else{
for (i = 0; i <level; i++) { // locate the position source button is in
for(j=0;j<level;j++){
if(this.XueButtonArray[i][j].getTileNumber().equals(eButton.getTileNumber())){
XueNum[0]=i;
XueNum[1]=j;
break;
}
}
}
for (n = 0; n <level; n++) { // locate the position white button is in
for(m=0;m<level;m++){
if(this.XueButtonArray[n][m].getTileNumber().equals(""+0)){
XueNum[2]=n;
XueNum[3]=m;
break;
}
}
}
/* Check the position relationship between white and source button.
If they are just next to each other, exchange states and call check() method. */
int CheckNum= Math.abs((XueNum[0]*level+XueNum[1])-(XueNum[2]*level+XueNum[3]));
if (CheckNum==1 || CheckNum==level){
this.XueButtonArray[XueNum[2]][XueNum[3]].setTileNumber(""+XueButtonArray[XueNum[0]][XueNum[1]].getTileNumber());
  相关解决方案