2
我通過E(fx)clipse和Java場景生成器構建JavaFX應用程序。使用JavaFX將可執行jar限制在一個窗口中
基本功能是登錄窗口。登錄後,將打開新窗口,登錄窗口消失。現在它只是在原型階段。
當用完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'?再次,我所引用的這些文件位於'.jar'內,我該如何指向這個?我可能會在這裏丟失一些東西... – blo0p3r
這與您的jar位置無關!在罐子裏航行!當我說相對路徑時,這不是100%正確,因爲您可以擁有相對路徑,但在解析罐子內部時,路徑中不能有「..」。 您可以使用getClass()。getClassloader()。getResource(「/ my/view/selector.fxml」)代替Classloader中的絕對位置。 – tomsontom
這是我錯過的知識。感謝您抽出寶貴時間解釋一切。 – blo0p3r