从不扩展Application的类的Main方法启动JavaFX [英] Starting JavaFX from Main method of class which doesn't extend Application

查看:3722
本文介绍了从不扩展Application的类的Main方法启动JavaFX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了从类的Main方法启动JavaFX应用程序的问题,该方法没有扩展 javafx.application.Application



在我的应用程序中有 MainApp.java ,它应该启动覆盖方法 start() MainUIController.java 中,扩展 Applciation



<当我从 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

 公共类MainUIController扩展Application {

private Stage stage;
//私人用户登录用户;
私人最终双倍MINIMUM_WINDOW_WIDTH = 800.0;
私人最终双倍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(夏普);
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 ,我收到以下错误:

 线程mainjava中的异常.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)
引起: java.lang.IllegalStateException:仅在事件线程上允许此操作; 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更多


解决方案

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



文档有一个特别注释at this this


注意:此方法在JavaFX应用程序线程上调用


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



例如,考虑你有类 JavaFXMain 扩展应用程序

  class JavaFXMain扩展Application {...} 

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

  class Main {
...
public void s omeMethod(){
...
JavaFXMain.launch(JavaFXMain.class); //启动JavaFX应用程序
...
}
}

在你的情况下,你可以在 MainApp 的主要方法中尝试这样的事情:

  //如果你不打算传递任何参数,你可以删除args 
MainUIController.launch(MainUIController.class,args)


I'm having problem to start a JavaFX Application from a Main method of a class which doesn't extend javafx.application.Application

In my application there is the MainApp.java which should start the overriden method start() in the MainUIController.java, which extends Applciation

When I start the Main method from the MainUIController.java everything works fine.

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

After running the MainApp, I get the following Error :

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

解决方案

In addition to what Nejinx said, you must not directly call your start(), always call launch(), because it sets up the JavaFX environment, including creation of stage and calls start() passing the stage as an parameter to it.

The docs has a note specially stating this

NOTE: This method is called on the JavaFX Application Thread

The launch() can be called from any class, taking into consideration if the class is directly not extending javafx.application.Application, then you must pass the class extending it as an argument to the launch method.

For example, consider you have a class JavaFXMain which extends Application

class JavaFXMain extends Application {...}

You can use any other class, to start the JavaFX Application.

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

In your case, you can try something like this inside the main method of MainApp:

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

这篇关于从不扩展Application的类的Main方法启动JavaFX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆