我需要你的帮助。 我不断收到此异常:-
Exception in thread "main" java.lang.IllegalStateException: Keyboard must be created before you can read events
at org.lwjgl.input.Keyboard.next(Keyboard.java:467)
at oregon.src.Controller.<init>(Controller.java:15)
at oregon.client.Oregon.start(Oregon.java:19)
at oregon.client.Oregon.main(Oregon.java:84)
我不知道如何解决它。我早些时候发布了一个问题,有人帮助了我,但后来就导致了这个问题。 这是 (oregon.src.Controller) 的代码:
package oregon.src;
import oregon.client.Oregon;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
public class Controller {
public Oregon oregon;
public Controller(Oregon oregon) throws LWJGLException {
this.oregon = oregon;
while (Keyboard.next()) {
if (Keyboard.getEventKeyState()) {
if (Keyboard.getEventKey() == Keyboard.KEY_F11) {
oregon.setFullscreen();
}
}
}
}
}
这是 (oregon.client.Oregon) 的代码:
package oregon.client;
import oregon.src.Controller;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
public class Oregon {
public static DisplayMode normal = new DisplayMode(640, 640);
public Controller controller;
public static boolean fullscreen = false;
public void start() {
try {
controller = new Controller(this);
} catch (LWJGLException e1) {
e1.printStackTrace();
}
try {
create();
} catch (LWJGLException e) {
stop(e);
}
while (!Display.isCloseRequested()) {
events();
Display.update();
}
Display.destroy();
}
public void events() {
try {
new Controller(this);
} catch (LWJGLException e) {
e.printStackTrace();
}
}
public void setFullscreen() {
try {
if (!fullscreen) {
Display.setFullscreen(true);
fullscreen = true;
} else if (fullscreen) {
Display.setDisplayMode(normal);
fullscreen = false;
}
} catch (LWJGLException e) {
stop(e);
}
}
public static void create() throws LWJGLException {
if (fullscreen) {
Display.setFullscreen(true);
} else if (!fullscreen) {
Display.setDisplayMode(normal);
}
Keyboard.create();
Display.create();
}
public void stop() {
System.exit(0);
Display.destroy();
}
public void stop(Exception e) {
e.printStackTrace();
System.exit(0);
Display.destroy();
}
public static void main(String args[]) {
Oregon oregon = new Oregon();
oregon.start();
}
}
如果有人能帮助我,我将不胜感激。 谢谢。
编辑:- 现在我收到此错误。
Exception in thread "main" java.lang.IllegalStateException: Display must be created.
at org.lwjgl.input.Keyboard.create(Keyboard.java:319)
at oregon.src.Controller.<init>(Controller.java:19)
at oregon.client.Oregon.start(Oregon.java:19)
at oregon.client.Oregon.main(Oregon.java:84)
最佳答案
正如错误所述 - 您需要创建键盘
public Controller(Oregon oregon) throws LWJGLException {
this.oregon = oregon;
Keyboard.create(); // <- add this here.
while (Keyboard.next()) {
if (Keyboard.getEventKeyState()) {
if (Keyboard.getEventKey() == Keyboard.KEY_F11) {
oregon.setFullscreen();
}
}
}
}
来自API :
"Create" the keyboard. The display must first have been created. The reason for this is so the keyboard has a window to "focus" in.
编辑:似乎您无法同时初始化它们,因此请尝试使用此方法 - 在 if 语句中覆盖初始化 Keyboard
:
if ( !Keyboard.isCreated() && !getPrivilegedBoolean("org.lwjgl.opengl.Display.nokeyboard") ) {
try {
Keyboard.create();
} catch (LWJGLException e) {
if ( LWJGLUtil.DEBUG ) {
e.printStackTrace(System.err);
} else {
LWJGLUtil.log("Failed to create Keyboard: " + e);
}
}
}
Here is posted similar source code, that seems to be working fine.
关于java - 我在 LWJGL 中使用键盘时不断收到此错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11071614/