JavaFX Fenster über statische Methode in eigener Klasse erzeugen und aufrufen



  • Hallo zusammen. Ich stehe momentan vor einem Problem bei dem ich nicht weiter komme. Ich möchte gerne in JavaFX eine eigene Klasse schreiben mit der ich über eine statische Methode ein neues Fenster erzeugen und ein sich darin befindliches Label initialisieren kann. Momentan habe ich folgenden Code.

    public class Main
    {
        public static void main(String argv[])
        {
           Dialog.showText("Hallo");
        }
    }
    
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    
    public class Dialog extends Application
    {
        private static Label label;
        private static VBox root = new VBox();
    
        public static void showText(String text)
        {
            label = new Label(text);
    
            root.getChildren().add(label);
    
            launch();
        }
    
        public void start(Stage stage)
        {
            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.setTitle("TEST");
            stage.show();
        }
    }
    

    Wenn ich das ganze Ausführe erhalte ich folgende Fehlermeldungen :

    Exception in thread "main" java.lang.ExceptionInInitializerError
    	at Dialog.showText(Dialog.java:16)
    	at Main.main(Main.java:7)
    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)
    	... 2 more
    

    Kann mir einer weiterhelfen?


Anmelden zum Antworten