在Java中使用JavaFX中的命令行参数 [英] Using command line arguments in Java with JavaFX
本文介绍了在Java中使用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类的方法中使用命令行)
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
推荐答案
尝试 getParameters 。这应该给你命令行参数
Try getParameters. This should give you the command line arguments
希望一个小例子(我从拉斐尔的回答中获取主代码)
As wished a small example (i took the main code from Raphael's answer)
假设控制器类名为MyController
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);
}
}
这篇关于在Java中使用JavaFX中的命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文