【问题标题】:How to close current stage when clicking a button to display new stage without making the components of the new stage inactive单击按钮以显示新阶段而不使新阶段的组件处于非活动状态时如何关闭当前阶段
【发布时间】:2018-02-04 09:11:17
【问题描述】:

假设我有一个StageA,其中包含两个Buttons、ouiButton 打开一个新的Stage Bnon 关闭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,它成功了!谢谢大佬,谢谢!
  • 写一个答案并接受它。
  • 我刚做了,过两天就可以接受了。

标签: java events javafx stage


【解决方案1】:

据我了解,您需要关闭当前阶段并打开新阶段。如果您需要关闭阶段A,我们可以说当前阶段并打开新阶段,我们可以说下一阶段,您需要获取您的活动窗口并关闭它,我的意思是 clickEvent

的窗口
 @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.show();
    nonBtnClick(event);


}

@FXML
private void nonBtnClick(ActionEvent event) {
  ((Node) event.getSource()).getScene().getWindow().hide();//close currentstage
}

【讨论】:

  • 您刚刚将showAndWait() 更改为show(),我在答案中说明了这一点,至于您的最后一行,它与nonBtnClick() 的作用相同。不过还是谢谢
【解决方案2】:

使用show 而不是showAndWait 就可以了。感谢 Sedrick Jefferson 的评论。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-20
    • 2012-08-25
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多