如何在java fx中出现新窗口时给出淡入淡出的背景?

时间:2014-04-07 02:29:10

标签: javafx javafx-2 fade javafx-8

我希望在出现新窗口时带来淡入淡出效果。如果不关闭窗户,也不可能做任何事情。我在下面给出按钮按下时打开新窗口的代码:

    Button b4 = new Button("ABOUT");
    b4.setFont(Font.font("Calibri", FontWeight.BOLD, 17));
    b4.setPrefSize(100, 30);
    b4.setStyle(" -fx-base: #ffffff;");
    b4.setTextFill(Color.BLACK);

    b4.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent e) {
            Stage usrpagestage = new Stage();
            usrpagestage.setMaxHeight(160);
            usrpagestage.setMaxWidth(210);
            usrpagestage.setResizable(false);
            usrpagestage.initStyle(StageStyle.UTILITY);
            usrpagestage.setScene(new Scene(new About()));
            usrpagestage.show();
        }
    });

我的2个窗口的当前外观如下所示。我只想让小窗口可见,其余的应该显得褪色。我该怎么办?

enter image description here

1 个答案:

答案 0 :(得分:4)

试试这个..

       b4.setOnAction(new EventHandler<ActionEvent>() {
       @Override
       public void handle(ActionEvent e) {

       //Before  open a add effect here


        anchpane.setEffect(new BoxBlur(5, 10, 10)); // anchpane is anchor pane of main stage. change values of efect according your need. you can use any kind of pane of scene.  

        Stage usrpagestage = new Stage();
        usrpagestage.setMaxHeight(160);
        usrpagestage.setMaxWidth(210);
        usrpagestage.setResizable(false);
        usrpagestage.initStyle(StageStyle.UTILITY);
        usrpagestage.setScene(new Scene(new About()));
        usrpagestage.show();
    }
});

看起来像:

enter image description here

关闭舞台时,请将其设为默认值。

 usrpagestage.setOnCloseRequest(new EventHandler<WindowEvent>() {

        @Override
        public void handle(WindowEvent t) {
         anchpane.setEffect(new BoxBlur(0, 0, 0));

        }
    });