简体   繁体   English

通过单击按钮关闭模式窗口

[英]Closing a modal window by clicking button

I have a window with a button. 我有一个带有按钮的窗口。 Clicking this button opens a modal window. 单击此按钮将打开一个模式窗口。 Now, I want to close this second window by clicking a button, but I can't figure out how. 现在,我想通过单击一个按钮来关闭第二个窗口,但是我不知道如何操作。

public class StartMenu extends Application {    
@Override
public void start(Stage primaryStage) {
final Button b = new Button("Go");
b.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent t) {               
           Stage stage = new Stage();
           stage.initModality(Modality.APPLICATION_MODAL);
           AnotherWindow aw = new AnotherWindow ();               
           aw.start(stage);                
        }
    });
((Group) scene.getRoot()).getChildren().add(b);
primaryStage.setScene(scene);        
primaryStage.show();
}}

public class AnotherWindow extends Application {    
@Override
public void start(Stage primaryStage) {
final Button b = new Button("Back");
b.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent t) {               
           //Code to close window               
        }
    });
((Group) scene.getRoot()).getChildren().add(b);
primaryStage.setScene(scene);        
primaryStage.show();
}}

I found the following post by Krzysztof Sz. 我发现了Krzysztof Sz的以下帖子。 that helped me find the solution. 这帮助我找到了解决方案。

public class AnotherWindow extends Application {    
    @Override
    public void start(Stage primaryStage) {
    final Button b = new Button("Back");
    b.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent t) {               
           ((Button)t.getTarget()).getScene().getWindow().hide();              
        }
    });
    ((Group) scene.getRoot()).getChildren().add(b);
    primaryStage.setScene(scene);        
    primaryStage.show();
    }}

It is the following piece of code that let's me close the current (modal) window when the button is clicked: 这是下面的代码,让我在单击按钮时关闭当前(模态)窗口:

    ((Button)t.getTarget()).getScene().getWindow().hide();

You want to close a modal window from a click on a different window? 您想通过单击其他窗口来关闭模式窗口吗? If a modal window is visible, how will you get back to the other window? 如果可见一个模式窗口,您将如何回到另一个窗口?

You might want to use one window: when a button is clicked, hide all the controls in that window and make visible the information you wanted to have in your modal window, along with a button to click. 您可能要使用一个窗口:单击一个按钮时,隐藏该窗口中的所有控件,并使您希望在模态窗口中拥有的信息以及单击的按钮可见。 When that button is clicked, reset the window to its' original state. 单击该按钮后,将窗口重置为其原始状态。

This just becomes an exercise in showing/hiding controls in a container. 这只是成为显示/隐藏容器中控件的一种练习。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM