【发布时间】:2015-10-01 13:10:46
【问题描述】:
我正在编写一些基本的 Java 游戏代码,但我找不到解决问题的方法。 代码如下:
[GameWindow.java]
package my.project.gop.main;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import javax.swing.JFrame;
public class GameWindow extends JFrame{
boolean fse = false;
int fsm = 0;
GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[1];
public GameWindow(String title, int width, int height) {
setSize(width, height);
setLocationRelativeTo(null);
setTitle(title);
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");
}
}
}
[Main.java]
package my.tdl.main;
import my.project.gop.main.GameWindow;
public class Main {
public static void main(String[] args) {
GameWindow frame = new GameWindow("TheDLooter", 1280, 720);
frame.setFullscreen(1);
frame.setVisible(true);
}
}
我确实收到此错误消息:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at my.project.gop.main.GameWindow.<init>(GameWindow.java:12)
at my.tdl.main.Main.main(Main.java:8)
【问题讨论】:
-
问题一定出在这里 -
device = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[1];。你不能假设数组至少有两个元素。 -
Java 数组从零开始。尝试将 1 更改为 0。
-
很明显,java中的数组从0开始,编译器清楚地说明了哪一行存在问题。我认为这是微不足道的。
-
@c4k3bomb,请您将解决您问题的答案标记为已接受的答案。