gpt4 book ai didi

inheritance - 从不扩展 Application 的类的 Main 方法启动 JavaFX

转载 作者:太空宇宙 更新时间:2023-11-04 14:29:21 24 4
gpt4 key购买 nike

我在从不扩展javafx.application.Application的类的Main方法启动JavaFX应用程序时遇到问题

在我的应用程序中,有一个 MainApp.java ,它应该启动 MainUIController.java 中的重写方法 start() ,该方法扩展了申请

当我从 MainUIController.java 启动 Main 方法时,一切正常。

MainApp.java

public class MainApp {

public static void main(String[] args) {
PersonJDBCTemplate jdbc = connect();
MainUIController mUIc = new MainUIController(jdbc);
mUIc.start(new Stage());

}

public static PersonJDBCTemplate connect() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"Beans.xml");
PersonJDBCTemplate personJDBCTemplate = (PersonJDBCTemplate) context
.getBean("personJDBCTemplate");
return personJDBCTemplate;
}
}

MainUIController.java

public class MainUIController extends Application {

private Stage stage;
// private User loggedUser;
private final double MINIMUM_WINDOW_WIDTH = 800.0;
private final double MINIMUM_WINDOW_HEIGHT = 570.0;
private String version = "0.6";
private PersonJDBCTemplate jdbc;

public MainUIController(PersonJDBCTemplate jdbc) {
this.jdbc = jdbc;

}

@Override
public void start(Stage primaryStage) {
try {
stage = primaryStage;
stage.setTitle("Sharp");
stage.setMinWidth(MINIMUM_WINDOW_WIDTH);
stage.setMinHeight(MINIMUM_WINDOW_HEIGHT);
stage.setResizable(false);
gotoLogin();
primaryStage.show();
} catch (Exception ex) {
Logger.getLogger(MainUIController.class.getName()).log(
Level.SEVERE, null, ex);
}
}

public void gotoLogin() {
try {
LoginController login = (LoginController) replaceSceneContent("/fxml/Login.fxml");
login.setApp(this);
} catch (Exception ex) {
Logger.getLogger(MainUIController.class.getName()).log(
Level.SEVERE, null, ex);
}
}
}

运行MainApp后,我收到以下错误:

Exception in thread "main" java.lang.ExceptionInInitializerError
at javafx.stage.Window.<init>(Window.java:1110)
at javafx.stage.Stage.<init>(Stage.java:236)
at javafx.stage.Stage.<init>(Stage.java:224)
at ch.kit.sharp.main.MainApp.main(MainApp.java:15)
Caused by: java.lang.IllegalStateException: This operation is permitted on the event thread only; currentThread = main
at com.sun.glass.ui.Application.checkEventThread(Application.java:445)
at com.sun.glass.ui.Screen.setEventHandler(Screen.java:245)
at com.sun.javafx.tk.quantum.QuantumToolkit.setScreenConfigurationListener(QuantumToolkit.java:600)
at javafx.stage.Screen.<clinit>(Screen.java:80)
... 4 more

最佳答案

除了Nejinx所说的之外,你不能直接调用你的start(),总是调用launch() ,因为它设置了JavaFX环境,包括创建阶段调用start()将阶段作为参数传递给它。

The docs有一个注释专门说明了这一点

NOTE: This method is called on the JavaFX Application Thread

launch()可以从任何类调用,考虑到该类是否直接扩展 javafx.application.Application ,那么您必须将扩展它的类作为参数传递给启动方法。

例如,假设您有一个类 JavaFXMain ,它扩展了 Application

class JavaFXMain extends Application {...}

您可以使用任何其他类来启动 JavaFX 应用程序。

class Main {
...
public void someMethod() {
...
JavaFXMain.launch(JavaFXMain.class); // Launch the JavaFX application
...
}
}

就您而言,您可以在 MainApp 的 main 方法中尝试类似的操作:

// You may remove args if you don't intend to pass any arguments
MainUIController.launch(MainUIController.class, args)

关于inheritance - 从不扩展 Application 的类的 Main 方法启动 JavaFX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26328692/

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