gpt4 book ai didi

java - 工具包未初始化 Java FX

转载 作者:行者123 更新时间:2023-12-01 09:32:23 24 4
gpt4 key购买 nike

当我仅以“运行”方式运行 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

我读过您应该子类化应用程序,但我已经这样做了,所以我不确定为什么它不起作用...如果我调试它工作正常,但一旦我尝试运行应用程序而不是调试它,它抛出该错误消息。这有点疯狂......有人知道到底发生了什么事吗?这是代码。

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();
}
}
}

最佳答案

Application 类中有一个 'public void init() throws Exception' 方法,其文档如下:

“应用程序初始化方法。加载和构造 Application 类后立即调用此方法。应用程序可以覆盖此方法以在应用程序实际启动之前执行初始化”。 “注意:此方法不会在 JavaFX 应用程序线程上调用。应用程序不得在此方法中构造场景或舞台。应用程序可以在此方法中构造其他 JavaFX 对象。”

所以,我认为你应该移动:p = new Player();textArea = new TextArea(); 到它。

关于java - 工具包未初始化 Java FX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39301681/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com