如何在单击按钮显示新阶段时关闭当前阶段而不使新阶段的组件处于非活动状态
How to close current stage when clicking a button to display new stage without making the components of the new stage inactive
假设我有一个 Stage
A
,其中包含两个 Button
,oui
Button
打开一个新的 Stage
B
,non
关闭 A
。我想要做的是在单击 oui
Button
并打开 B
后关闭 A
。我正在使用 showAndWait()
打开 B
,然后我尝试执行 A.close()
,当我尝试 运行 A.close()
在 showAndWait()
A
关闭之前,但是所有 B
控件包括 Button
s 和 Text Field
s 都变得不活动,有什么解决方法吗?
这是点击 oui
以打开 B
Stage
时执行的代码:
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();
}
}
使用 show
而不是 showAndWait
就成功了。感谢 Sedrick Jefferson 的评论。
根据我的理解,你需要关闭当前阶段并打开新的stage.If你需要关闭阶段A,我们可以说当前阶段并打开新阶段,我们可以说下一个阶段,你需要得到window 你的事件并关闭它,我的意思是 window of 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
}
假设我有一个 Stage
A
,其中包含两个 Button
,oui
Button
打开一个新的 Stage
B
,non
关闭 A
。我想要做的是在单击 oui
Button
并打开 B
后关闭 A
。我正在使用 showAndWait()
打开 B
,然后我尝试执行 A.close()
,当我尝试 运行 A.close()
在 showAndWait()
A
关闭之前,但是所有 B
控件包括 Button
s 和 Text Field
s 都变得不活动,有什么解决方法吗?
这是点击 oui
以打开 B
Stage
时执行的代码:
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();
}
}
使用 show
而不是 showAndWait
就成功了。感谢 Sedrick Jefferson 的评论。
根据我的理解,你需要关闭当前阶段并打开新的stage.If你需要关闭阶段A,我们可以说当前阶段并打开新阶段,我们可以说下一个阶段,你需要得到window 你的事件并关闭它,我的意思是 window of 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
}