Java ArrayIndexOutOfBoundsException 2D游戏开发
我一直在关注这一点,但我不知道错误在哪里。接下来的2节课正在解释每一件事,因为只有2节课我试图再次观看教程,但仍然没有发现错误Java ArrayIndexOutOfBoundsException 2D游戏开发,java,2d-games,Java,2d Games,我一直在关注这一点,但我不知道错误在哪里。接下来的2节课正在解释每一件事,因为只有2节课我试图再次观看教程,但仍然没有发现错误 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package gameofthrones;
import java.awt.*;
import javax.swing.*;
/**
*
* @author issba
*/
public class ClassOGP extends JFrame{
boolean fse =false;
int fsm = 0;
GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[1];
public ClassOGP(String title,int width,int height){
setTitle(title);
setSize(width,height);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
}
private void setfullscreen(){
switch(fsm){
case 0:
System.out.println("No fullscreen");
setUndecorated(false);
break;
case 1:
setExtendedState(JFrame.MAXIMIZED_BOTH);
setUndecorated(true);
break;
case 2:
device.setFullScreenWindow(this);
setUndecorated(true);
break;
}
}
public void setFullscreen(int fsnm){
fse = true;
if(fsm <= 2){
this.fsm = fsnm;
setfullscreen();
}else{
System.err.println("Error " + fsnm + " is not supported");
}
}
}
此处显示错误消息
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at gameofthrones.ClassOGP.<init>(ClassOGP.java:18)
at gameofthrones.Main.main(Main.java:20)
C:\Users\issba\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)
线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:1
ClassOGP.(ClassOGP.java:18)
在gameofthrones.Main.Main(Main.java:20)
C:\Users\issba\AppData\Local\NetBeans\Cache\8.1\executor snippets\run.xml:53:Java返回:1
生成失败(总时间:1秒)
当您试图访问具有非法数组索引或超出数组范围的元素时,会发生错误。此行:
GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[1];
正在抛出错误。尝试将1
更改为0
不过,这只是一个快速解决方案,您应该将设备声明为实例或类成员,并在构造函数中分配它。然后,如果没有屏幕设备,您可以执行错误检查。例如:
public class ClassOGP extends JFrame{
/* other code */
public GraphicsDevice device;
public ClassOGP(String title,int width,int height) {
if(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices().length() > 0) {
// you can also check for multiple devices here to see if you want
// to use one other than the zero'th index
device = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0];
} else {
System.out.println("ERROR: No devices ... exiting.");
System.exit();
}
/* other code */
}
/* rest of class */
}
此行:
GraphicsDevice设备=GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[1]代码>正在抛出错误。尝试将1
更改为0
@JonnyHenly right谢谢你,如果你愿意,你可以将其作为答案发布,也可以将不同的plz@Siddharth谢谢你的编辑谢谢你的添加,但我还是新手,我正在关注教程,所以谢谢你的帮助,我为你添加了+1和+1的学分将我的答案标记为已接受。如果另一个用户提供了更多的见解并添加了更好的答案,我不介意你更改他们的答案是否被接受。你知道如何让我的代码(相同的代码)也将数字1用作设备吗?这取决于你有多少屏幕设备,显然你只有一个屏幕设备。因为当您尝试访问返回数组的索引1时,它会抛出一个索引越界异常。数组索引从0开始,因此第一个元素将位于索引0,第二个元素将位于索引1。
public class ClassOGP extends JFrame{
/* other code */
public GraphicsDevice device;
public ClassOGP(String title,int width,int height) {
if(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices().length() > 0) {
// you can also check for multiple devices here to see if you want
// to use one other than the zero'th index
device = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0];
} else {
System.out.println("ERROR: No devices ... exiting.");
System.exit();
}
/* other code */
}
/* rest of class */
}