我在跟踪这个教程,但是我不知道错误在哪里,下面的两个类解释了每一件事情,因为它只是两个类,我又试着看了一遍教程,但仍然没有找到错误
/*
* 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");
}
}
}
这是主要的类,里面没有多少代码。
/*
* 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;
/**
*
* @author issba
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ClassOGP frame = new ClassOGP("Game Of thrones",1280,720);
frame.setFullscreen(1);
frame.setVisible(true);
}
}
这里的错误信息
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)
发布于 2016-05-05 02:05:30
这一行:
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 */
}
发布于 2016-05-05 02:05:22
当您试图访问具有非法数组索引或不在数组范围之外的元素时,会发生此错误。
https://stackoverflow.com/questions/37041052
复制相似问题