我使用JavaFX是因为我必须编写一个“交互式shell”。我已经编写了通过SSH连接到远程主机的Class Shell。
但是Shell在他的构造函数中有三个参数:
public Shell(String username, String password, String host) {
this.username = username;
this.password = password;
this.host = host;
}
我需要(或将是完美的):
@Override
public void start(Stage primaryStage,String username, String password, String host) {
this.primaryStage = primaryStage;
this.shell = new Shell(username,password,host);
initialiseOverview();
}
有没有办法可以在JavaFX start方法中添加额外/可选的起始参数。或者还有其他方法可以解决这个问题吗?
提前谢谢你:)
答案 0 :(得分:3)
您可以使用Application.getParameters()
访问命令行参数:
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
Application.Parameters parameters = getParameters();
List<String> rawParams = parameters.getRaw();
String userName = rawParams.get(0);
String password = rawParams.get(1);
String host = rawParams.get(2);
this.shell = new Shell(username,password,host);
initialiseOverview();
}
此代码假定(至少)有三个命令行参数。您可能希望添加对参数数量的检查,并显示错误消息或提示是否缺失等等。