java 在 JavaFX 中使用命令行参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36032888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:54:20  来源:igfitidea点击:

Using command line arguments in Java with JavaFX

javajavafxscenebuilder

提问by Karatawi

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

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

回答by Clayn

Try getParameters. This should give you the command line arguments

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

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

正如希望的一个小例子(我从拉斐尔的回答中获取了主要代码)

Assuming the controller class is named "MyController"

假设控制器类被命名为“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);
 }
}