如何在应用程序 javafx 的 start 方法中传递参数? 事实上,我在另一个类中通过以下行调用我的 javafx 应用程序:
mainController.getMainApp().launch(MainApp.class);
下面是我的启动方法:
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("CLS");
this.primaryStage.initStyle(StageStyle.UNDECORATED);
// here the page fxml to load
问题是我不知道我将在启动方法中加载的页面 fxml 是什么。这是我另一个类的人知道的。 所以我想在我的方法中传递参数开始知道我将加载什么 fxml 页面。
请帮忙。
谢谢。
最佳答案
launch
Application
的方法被设计为接受 varargs 参数:
public static void launch(Class<? extends Application> appClass, String... args)
因此您可以传递如下参数:
launch(MainApp.class, "--FXMLFile=Sample.fxml", "UnnamedArgument");
然后在 init
或在 start
方法,您可以使用 getParameters()
获取这些参数方法:
Parameters parameters = getParameters();
List<String> unnamed = parameters.getUnnamed();
Map<String, String> named = parameters.getNamed();
List<String> raw = parameters.getRaw();
您可以使用命名或未命名参数(在示例中,第一个参数已命名,第二个参数未命名。getNamed()
方法返回 Map
中的命名参数,getUnnamed()
方法返回 List
中的未命名参数,而 getRaw()
将命名和未命名参数作为原始列表
返回。
getParameters() 的 javadoc 注释:
NOTE: this method should not be called from the Application constructor, as it will return null. It may be called in the init() method or any time after that.
关于java - 添加参数启动方法 JavaFX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38499865/