【发布时间】:2018-02-04 09:11:17
【问题描述】:
假设我有一个StageA,其中包含两个Buttons、ouiButton 打开一个新的Stage B、non 关闭A。我要做的是在点击ouiButton 并打开B 后关闭A。我正在使用showAndWait() 打开B,然后我尝试执行A.close(),这显然失败了showAndWait(),当我尝试在showAndWait() A 关闭之前运行A.close(),但随后包括Buttons 和Text Fields 在内的所有Bcontrols 都变为非活动状态,是否有任何解决方法?
这是点击oui打开BStage时执行的代码:
public class AController implements Initializable {
@FXML
private Button oui;
@FXML
private Button non;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
@FXML
private void ouiBtnClick(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("B.fxml"));
Stage stage = new Stage();
VBox mainPane = (VBox) loader.load();
Scene scene = new Scene(mainPane);
stage.setScene(scene);
stage.initStyle(StageStyle.DECORATED);
stage.setResizable(false);
stage.showAndWait();
nonBtnClick(); // method that close `A`
}
@FXML
private void nonBtnClick() {
Stage s = (Stage) non.getScene().getWindow();
s.close();
}
}
【问题讨论】:
-
This 应该证明你在问什么。
-
另外,如果你使用
show而不是showAndWait会发生什么? -
@SedrickJefferson 我使用了
show,它成功了!谢谢大佬,谢谢! -
写一个答案并接受它。
-
我刚做了,过两天就可以接受了。