Using command line arguments in Java with JavaFX

落爺英雄遲暮 提交于 2019-11-28 02:59:19

问题


I have the following code:

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

I want to know how you would use a file (given with the command line) in the Controller or in a method in the Main class


回答1:


Try getParameters. This should give you the command line arguments

As wished a small example (i took the main code from Raphael's answer)

Assuming the controller class is named "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);
 }
}


来源:https://stackoverflow.com/questions/36032888/using-command-line-arguments-in-java-with-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!