1 回答

TA貢獻2065條經驗 獲得超14個贊
您正在檢查!stage.isShowing()在新創建的 Stage。這永遠不會做你想做的。您需要保留對另一個的引用Stage并繼續使用該引用。
public class Controller {
private Stage otherStage;
@FXML
private void btn_Validate(ActionEvent event) {
if (otherStage == null) {
Parent root = ...;
otherStage = new Stage();
otherStage.setScene(new Scene(root));
otherStage.show();
} else if (otherStage.isShowing()) {
otherStage.toFront();
} else {
otherStage.show();
}
}
}
如果您不想Stage在關閉時將其保留在內存中,那么您可以稍微更改上述內容。
public class Controller {
private Stage otherStage;
@FXML
private void btn_Validate(ActionEvent event) {
if (otherStage == null) {
Parent root = ...;
otherStage = new Stage();
otherStage.setOnHiding(we -> otherStage = null);
otherStage.setScene(new Scene(root));
otherStage.show();
} else {
otherStage.toFront();
}
}
}
根據您的需要,您可能還想存儲對已加載控制器的引用。
添加回答
舉報