简体   繁体   English

如何动态初始化JavaFX应用程序,而不是硬编码?

[英]How to initialize JavaFX application dynamically, not hardcoded?

In many samples it is shown how to extend Application method to have JavaFX app composed and ran. 在许多示例中,它展示了如何扩展Application方法以组成和运行JavaFX应用程序。

But what if I don't want to? 但如果我不想怎么办? What if I want to configure app dynamically from my code? 如果我想从我的代码动态配置应用程序怎么办? Example is below: 示例如下:

import javafx.application.Application;
import javafx.stage.Stage;

public class HollowTry {

   public static class HollowApplication extends Application {
      @Override
      public void start(Stage primaryStage) throws Exception {
      }
   }

   public static void main(String[] args) {
      Application.launch(HollowApplication.class, args);

      // now I want to set title, scene etc... how?

   }

}

Please don't dispute on why I need it. 请不要就我为何需要它提出异议。

UPDATE UPDATE

Okay, launch() is never terminated, I didn't check this. 好的, launch()永远不会终止,我没有检查过。 Anyway, I need to have a way to build application programmatically, without any hardcoding. 无论如何,我需要有一种方法以编程方式构建应用程序,而无需任何硬编码。

UPDATE 2 更新2

I was wishing con build application from Spring and I found the following solution for now. 我希望从Spring构建应用程序,我现在找到了以下解决方案。

JavaFX wrapper class JavaFX包装类

It wraps context initialization into FX thread and captures config classes to be accessible from start() : 它将上下文初始化包装到FX线程中,并捕获可从start()访问的配置类:

public class SpringJavaFX extends Application {

   private static Class<?>[] annotatedClasses;

   @Override
   public void start(Stage primaryStage) throws Exception {

      AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(annotatedClasses);

      String title = (String) context.getBean("primaryStageTitle");
      primaryStage.setTitle(title);

      Scene scene = (Scene) context.getBean("primaryStageScene");
      primaryStage.setScene(scene);

      primaryStage.show();

   }

   public static void launch(Class<?>... annotatedClasses) {
      SpringJavaFX.annotatedClasses = annotatedClasses;
      Application.launch();

   }
}

Spring way building 春天的方式建设

And here is an example of spring-way building. 这里是春天建筑的一个例子。 Each component is a bean and created in place: 每个组件都是一个bean并在适当的位置创建:

public class Attempt01_HelloWorld {

   @Configuration
   public static class Config {

      @Bean
      String primaryStageTitle() {
         return "Attempt01_HelloWorld";
      }

      @Bean
      Scene primaryStageScene() {
         Scene ans = new Scene(root(), 800, 600);
         return ans;
      }

      @Bean
      Button button() {
         Button ans = new Button();
         ans.setText("Say 'Hello World'");
         ans.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
               System.out.println("Hello World!");
            }
         });
         root().getChildren().add(ans);
         return ans;
      }

      @Bean
      StackPane root() {
         StackPane root = new StackPane();
         return root;
      }


   }

   public static void main(String[] args) {
      SpringJavaFX.launch(Config.class);
   }

}

I'm not sure it would work, but you could try and add setter methods for the app's parameters inside the inner class, and try and call them from outside (eg from your main). 我不确定它是否会起作用,但您可以尝试在内部类中为应用程序的参数添加setter方法,并尝试从外部调用它们(例如从主体中调用它们)。 Again, I don't know whether this would work or not, but I'd give it a try in your place. 同样,我不知道这是否会起作用,但我会在你的位置尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM