当我将JavaFx应用程序仅运行为“运行”时,我收到了以下错误。调试工作正常......
Exception in thread "main" java.lang.ExceptionInInitializerError
at Goose.Program.<clinit>(Program.java:26)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:123)
Caused by: java.lang.IllegalStateException: Toolkit not initialized
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:273)
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:268)
at com.sun.javafx.application.PlatformImpl.setPlatformUserAgentStylesheet(PlatformImpl.java:550)
at com.sun.javafx.application.PlatformImpl.setDefaultPlatformUserAgentStylesheet(PlatformImpl.java:512)
at javafx.scene.control.Control.<clinit>(Control.java:87)
... 4 more
我已经读过你应该将Application子类化,但我已经这样做了,所以我不确定它为什么不起作用...如果我调试但是只要我尝试运行应用程序而不是调试它就可以正常工作它,它会抛出该错误消息。这有点疯狂......任何人都知道到底发生了什么事?这是代码。
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class Program extends Application{
TextField input;
GameServer gm;
Player p = new Player();
/**
* Just starts our GameServer
*/
public static void main(String[] args) throws Exception {
launch(args);
}
public static final TextArea textArea = new TextArea();
@Override
public void start(Stage primaryStage) {
p.setState(Player.States.Ready);
p.setAccess(Player.AccessStatus.GameMaster);
input = new TextField();
input.setPrefWidth(500);
input.setOnKeyPressed(event -> {
if(event.getCode().equals(KeyCode.ENTER)){
textArea.appendText("Command: " + input.getText() + "\n");
handleEvent(input);
input.setText("");
}
});
GridPane gridPane = new GridPane();
gridPane.setAlignment(Pos.CENTER);
gridPane.setHgap(10);
gridPane.setVgap(10);
gridPane.add(input, 0, 0, 2, 1);
gridPane.add(textArea, 0,2, 2, 1);
Scene scene = new Scene(gridPane, 530, 250);
primaryStage.setMaxWidth(540);
primaryStage.setMaxHeight(280);
primaryStage.setMinWidth(540);
primaryStage.setMinHeight(280);
primaryStage.setTitle("My Server");
primaryStage.setScene(scene);
primaryStage.show();
primaryStage.setOnCloseRequest(we -> {
try {
textArea.appendText("Shutting down server...");
if(gm.gameworld.getRunning()) {
gm.gameworld.setRunning(false);
Thread.sleep(2000);
}
System.exit(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
run();
}
public void run(){
try {
GameServer gameServer = new GameServer();
this.gm = gameServer;
gameServer.start();
}catch (Exception e){
e.printStackTrace();
}
}
public void handleEvent(TextField textField){
try {
String eventKey = textField.getText().trim();
Event e = gm.gameworld.getEventHandler().stringToEvent.get(eventKey);
if(e != null) {
e.setPlayer(p);
e.ready(gm.gameworld);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
Application类中有一个'public void init() throws Exception'
方法,其文档说明:
&#34;应用程序初始化方法。在加载和构造Application类之后立即调用此方法。应用程序可以覆盖此方法,以便在实际启动应用程序之前执行初始化&#34;。 &#34;注意:不在 JavaFX 应用程序线程上调用此方法。应用程序不得在此方法中构造Scene或Stage。应用程序可以在此方法中构造其他JavaFX对象。&#34;
所以,我想你应该移动:p = new Player();
和
textArea = new TextArea();
到它。