我正在通过 E(fx)clipse 和 Java Scene Builder 构建 JavaFX 应用程序。
基本功能是登录窗口。登录后,将打开新窗口,然后登录窗口消失。目前还处于原型(prototype)阶段。
当 ecplise 用完时,我想要的功能都有了。登录窗口在启动时显示(代码如下)
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("view/login.fxml"), ResourceBundle.getBundle("ca.sportstats.resources.labels"));
primaryStage.setTitle("SportStats Live Update Tool : Login");
primaryStage.setScene(new Scene(root, 450, 300));
primaryStage.show();
} catch (IOException e) {
//Change this to open a small popup window.
System.out.println("Could not deploy");
}
}
public static void main(String[] args) {
launch(args);
}
此窗口上有一个按钮,只需打开另一个按钮(登录逻辑稍后会出现,这里不是问题)。
btnLogin.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
//TODO: Login logic.
//On success allow to open the tool (aka main window);
Parent root;
try {
root = FXMLLoader.load(getClass().getResource("../view/selector.fxml"), resources);
Stage stage = new Stage();
stage.setTitle("Selector");
stage.setScene(new Scene(root, 450, 450));
stage.show();
//hide this current window
((Node)(event.getSource())).getScene().getWindow().hide();
} catch (IOException e) {
e.printStackTrace();
}
}
});
这在 Ecplise 中没有问题。但!当我构建这个(以描述的方式 on the e(fx)clipse tutorials 时,我得到一个可执行 jar,但只得到登录窗口。当我单击按钮时,第二个窗口不会显示。
最佳答案
我认为问题是在 jar 中你不能做相对路径。在 Eclipse 内部,您运行在文件系统上,这不是问题
关于java - 使用 JavaFX 将可执行 Jar 限制为一个窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14241283/