我是javafx的新手,我需要您的幫助。我在互聯網上搜索,但沒有成功......所以,當我第一次使用jar時,我正在嘗試使對話框出現。要做到這一點,我試圖測試一個文件中是否存在某個屬性,如果是這樣,請啓動「首次對話框」。當我在這個對話框上點擊「確定」時,我將把屬性保存到文件中。唯一的問題,primaryStage不是第一次是「主窗口」。這個對話框有一個控制器,所以當我在按鈕的「setOnAction方法」中時,我無法啓動primaryStage。我不能使用「node.getWindow.getScene」(或類似的東西)方法,因爲它還沒有打開,用戶仍然在「第一時間窗口」。這裏是我的代碼..從JavaFX的Main類中顯示一個舞臺
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("real.fxml"));
primaryStage.setTitle("LoL Ready To Win");
primaryStage.setResizable(false);
primaryStage.setScene(new Scene(root));
try {
input = new FileInputStream("config.properties");
prop.load(input);
if(prop.getProperty("name") != null){
primaryStage.show();
}else if(prop.getProperty("name") == null){
try {
Stage stage = new Stage();
Parent root1 = FXMLLoader.load(getClass().getResource("summ.fxml"));
stage.setTitle("Insert Title Here");
stage.setResizable(false);
stage.setScene(new Scene(root1));
stage.show();
}catch(IOException e){
e.printStackTrace();
}
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
這是控制器
submit.setOnAction(e->{
if (summfield.getText() == null || summfield.getText().trim().isEmpty()) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Temp title");
alert.setHeaderText(null);
alert.setContentText("Please enter a valid name!");
alert.showAndWait();
}else{
try {
output = new FileOutputStream("config.properties");
prop.setProperty("name", summfield.getText());
prop.store(output, null);
} catch (IOException io) {
io.printStackTrace();
} finally {
if (output != null) {
try {
output.close();
} catch (IOException a) {
a.printStackTrace();
}
}
}
// And this is where I wanna launch the primaryStage
}
});
如果你有,即使它不是一個回答我的問題的任何意見,我不勝感激!謝謝大家。
這似乎很好地工作,但唯一的問題是,如果輸入沒有按」如果存在,它會給出錯誤。我會嘗試檢查它是否也存在...如果它有效,我會讓你知道。 –
非常感謝!這工作完美。我在輸入聲明前添加了一個「if(file.exists())」,就像這樣完美。謝謝! –