[英]Using command line arguments in Java with JavaFX
我有以下代碼:
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類的方法中使用文件(通過命令行給出)
試試getParameters 。 這應該給你命令行參數
希望一個小例子(我從拉斐爾的回答中獲取主要代碼)
假設控制器類名為“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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.