【问题标题】:Using command line arguments in Java with JavaFX在 Java 中通过 JavaFX 使用命令行参数
【发布时间】:2016-07-02 04:02:17
【问题描述】:

我有以下代码:

public class Main extends Application {

  @Override
  public void start(Stage primaryStage) throws Exception{
      Parent root = FXMLLoader.load(getClass().getResource("hive.fxml"));
      primaryStage.setTitle("Hive-viewer");
      primaryStage.setScene(new Scene(root, 1600, 900));
      primaryStage.show();
  }


  public static void main(String[] args) {
      launch(args);
  }
}

我想知道如何在 Controller 或 Main 类的方法中使用文件(通过命令行给出)

【问题讨论】:

  • 你在用什么命令行?

标签: java javafx scenebuilder


【解决方案1】:

试试getParameters。这应该给你命令行参数

如愿举一个小例子(我从 Raphael 的回答中获取了主要代码)

假设控制器类名为“MyController”

public class Main extends Application {

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

    FXMLLoader loader=new FXMLLoader(getClass().getResource("hive.fxml"));
    Parent root = loader.load();
    MyController cont=load.getController();
    /*
      This depends on your controller and you have to decide 
      How your controller need the arguments
    */
    cont.setParameter(getParameters()); 

    primaryStage.setTitle("Hive-viewer");
    primaryStage.setScene(new Scene(root, 1600, 900));
    primaryStage.show();
 }


 public static void main(String[] args) {
    launch(args);
 }
}

【讨论】:

  • 向这个答案添加一些细节可能会有所帮助,并举例说明如何将从方法调用中检索到的参数值传递给控制器​​。
  • @James_D 感谢您的建议。我希望它现在更好。我对 SO 不是很活跃。
猜你喜欢
  • 1970-01-01
  • 2014-04-03
  • 2012-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多