_斑马程序员 2018-09-18 15:51 采纳率: 0%
浏览 12083

java.lang.ArrayIndexOutOfBoundsException: -1

这个怎么解决啊 弄了两天啦小白一个 ,做了一个推箱子的做了一半出错做不下去了,求助

public class MainFrame extends Frame implements KeyListener {
public MainFrame() {

    tagetinit(); //笼子
    personinit();//人物
    Boxinit();//箱子
    Treeinit();//树
    backgroundInde();//背景初始化
    setMainFrameUI();//主窗体

    this.addKeyListener(this);  //键盘监听器







}
//用二维数组定义障碍
int[][] datas = {   
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
        {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
        {1,0,1,1,0,0,0,0,0,1,1,0,0,0,0,1},
        {1,0,1,1,0,0,0,0,0,1,1,0,0,0,0,1},
        {1,0,1,1,0,1,1,0,0,1,1,0,0,0,0,1},
        {1,0,1,1,0,1,1,0,0,1,1,0,0,0,0,1},
        {1,0,1,1,0,1,1,0,0,1,0,0,0,0,0,1},
        {1,0,1,1,0,1,1,0,0,0,0,0,0,0,0,1},
        {1,0,1,1,0,1,1,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1},
        {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
        {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
        };
int wx;
int wy;

    private void Treeinit() {
        Icon ic =new ImageIcon("tree.png");
        //二维数组的遍历

        for(int i=0;i<datas.length;i++) {
            for(int j=0;j<datas[i].length;j++) {
                if(datas[i][j] ==1) {
                JLabel lab_tree=new JLabel(ic);
                lab_tree.setBounds(12+50*j, 36+50*i, 50, 50);
                this.add(lab_tree);
                }
                }


        }

    }

 //笼子
private void tagetinit() {
    Icon i = new ImageIcon("lz.png");
    JLabel lab_lz =new JLabel(i);
    this.add(lab_lz);
    lab_lz.setBounds(612, 436, 50, 50);

    JLabel lab_lz2 =new JLabel(i);
    lab_lz2.setBounds(612, 486, 50, 50);
    this.add(lab_lz2);

    JLabel lab_lz3 =new JLabel(i);
    lab_lz3.setBounds(612, 386, 50, 50);
    this.add(lab_lz3);
}


//背景
private void backgroundInde() {
    Icon i =new ImageIcon("bg1.jpg");
    JLabel lag_bg = new JLabel(i);
    //设置位置与大小
    lag_bg.setBounds(12,36,800,600);
    this.add(lag_bg);
}

//窗口
private  void setMainFrameUI() {
    this.setLayout(null);// 设置自由布局
    this.setTitle("推箱子1.1");//设置窗口名称
    this.setLocation(300, 400); //窗口的位置
    this.setSize(850, 650); //窗口大小
    this.setVisible(true); //窗口可见
    }
// 人物
private void personinit() {
    int wx=1;
    int wy=8;
    Icon i = new ImageIcon("htl-zm.png");
    lab_person = new JLabel(i);  
    lab_person.setBounds(12+wx*50,36+wy*50,50,50 );    //设置位置
    this.add(lab_person); //添加

}   
JLabel lab_person;//本来该法放在方法里面  放在这里让变量的作用范围变大一级
//箱子
private void Boxinit() {
    Icon i=new ImageIcon("lyy.png");
    JLabel lab_box1= new JLabel(i);
    lab_box1.setBounds(212, 236, 50, 50);
    this.add(lab_box1);

    JLabel lab_box2= new JLabel(i);
    lab_box2.setBounds(412, 236, 50, 50);
    this.add(lab_box2);

    JLabel lab_box3= new JLabel(i);
    lab_box3.setBounds(612, 236, 50, 50);
    this.add(lab_box3);
    }

//键盘监听器
@Override
public void keyPressed(KeyEvent e) {
//左 37 上38 右39 下40
int key = e.getKeyCode(); //用于获取键码值
//右移
if(key == 39) {

        if(datas[wy][wx+1] == 1) {
            return;
            }

        wx=wx+1;

         int x=(int)lab_person.getLocation().getX(); 
         int y=(int)lab_person.getLocation().getY();
         lab_person.setLocation(x+50,y);
         Icon i = new ImageIcon("htl-right.png");
         lab_person.setIcon(i);
    }
    //左移
    if(key == 37) {
        if(datas[wy][wx-1] == 1) {
            return;
        }
        wx=wx-1;
        //让人物移动首先要知道人物的位置
         int x=(int)lab_person.getLocation().getX(); //得到的是走上叫
         int y=(int)lab_person.getLocation().getY();
         //确定一步移动多少
         //右移x增加 y不变
         lab_person.setLocation(x-50,y);
         //人物移动会改变方向 也就是改变一张图片
        Icon i = new ImageIcon("htl-left.png");
        lab_person.setIcon(i);
        }
    //上移
    if(key == 38) {
        if(datas[wy-1][wx] == 1) {
            return;
        }

        wy=wy-1;

         int x=(int)lab_person.getLocation().getX(); 
         int y=(int)lab_person.getLocation().getY();
         lab_person.setLocation(x,y-50);
         Icon i = new ImageIcon("htl-back.png");
         lab_person.setIcon(i);
    }
    //下移
    if(key == 40) {

        if(datas[wy+1][wx]==1) {
            return;


        }

        wy=wy+1;
         int x=(int)lab_person.getLocation().getX(); 
         int y=(int)lab_person.getLocation().getY();
         lab_person.setLocation(x,y+50);
         Icon i = new ImageIcon("htl-zm.png");
         lab_person.setIcon(i);
    }

}


@Override
public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub

}


@Override
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

}

}
  • 写回答

1条回答 默认 最新

  • IT清流李小白 2018-09-19 00:54
    关注

    for(int i=0;i<datas.length-1 ;i++) { 便利循环的时候 -1 你试试

    评论

报告相同问题?

悬赏问题

  • ¥15 ROS Turtlebot3 多机协同自主探索环境时遇到的多机任务分配问题,explore节点
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题