How to show and hide a window in JavaFX 2?

别等时光非礼了梦想. 提交于 2019-11-28 06:16:54

问题


Need to show an example in which you show and hide a window in JavaFX 2.


回答1:


A stage is a window in javafx-2. It provide the hide and show methods:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class HideShowApp extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    @Override
    public void start(Stage stage) throws Exception {
        final Stage window = new Stage();
        window.setX(10);
        Scene innerScene = new Scene(new Label("inner window"));
        window.setScene(innerScene);

        HBox root = new HBox(10d);
        Button showButton = new Button("show");
        showButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                window.show();
            }
        });
        Button hideButton = new Button("hide");
        hideButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                window.hide();
            }
        });
        root.getChildren().add(showButton);
        root.getChildren().add(hideButton);
        stage.setScene(new Scene(root));
        stage.show();
    }
}


来源:https://stackoverflow.com/questions/15520573/how-to-show-and-hide-a-window-in-javafx-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!