舞台底部的折叠菜单

时间:2014-09-05 13:58:51

标签: javafx javafx-8

基于此示例

https://gist.github.com/SaiPradeepDandem/95accfa4d8de8b9b3310

我创建了这个简单的示例代码:

import javafx.animation.Animation;
import javafx.animation.Transition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

public class MainApp extends Application
{
    BorderPane bp = new BorderPane();

    public static void main(String[] args) throws Exception
    {
        launch(args);
    }

    @Override
    public void start(final Stage stage) throws Exception
    {
        stage.setTitle("Slide out YouTube demo");

// create a WebView to show to the right of the SideBar.
        bp.setStyle("-fx-background-color: #2f4f4f;");
        bp.setPrefSize(800, 600);

// create a sidebar with some content in it.
        final Pane lyricPane = createSidebarContent();
        SideBar sidebar = new SideBar(250, lyricPane);
        VBox.setVgrow(lyricPane, Priority.ALWAYS);

// layout the scene.
        final BorderPane layout = new BorderPane();

        StackPane st = new StackPane();
        st.getChildren().addAll(bp, sidebar.getControlButton());
        st.setAlignment(Pos.TOP_LEFT);

        VBox vb = new VBox(10);
        vb.getChildren().addAll(st);

        layout.setLeft(sidebar);
        layout.setCenter(vb);

// show the scene.
        Scene scene = new Scene(layout);
        scene.getStylesheets().add(getClass().getResource("/styles/slideout.css").toExternalForm());
        stage.setScene(scene);
        stage.show();
    }

    private BorderPane createSidebarContent()
    {
// create some content to put in the sidebar.
        final Button changeLyric = new Button("New Song");
        changeLyric.getStyleClass().add("change-lyric");
        changeLyric.setMaxWidth(Double.MAX_VALUE);
        changeLyric.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent actionEvent)
            {
                System.out.println("Some action");
            }
        });
        changeLyric.fire();
        final BorderPane lyricPane = new BorderPane();
        lyricPane.setTop(changeLyric);
        return lyricPane;
    }

    /**
     * Animates a node on and off screen to the left.
     */
    class SideBar extends VBox
    {
        /**
         * @return a control button to hide and show the sidebar
         */
        public Button getControlButton()
        {
            return controlButton;
        }
        private final Button controlButton;

        /**
         * creates a sidebar containing a vertical alignment of the given nodes
         */
        SideBar(final double expandedWidth, Node... nodes)
        {
            getStyleClass().add("sidebar");
            this.setPrefWidth(expandedWidth);
            this.setMinWidth(0);

// create a bar to hide and show.
            setAlignment(Pos.CENTER);
            getChildren().addAll(nodes);

// create a button to hide and show the sidebar.
            controlButton = new Button("Collapse");
            controlButton.getStyleClass().add("hide-left");

// apply the animations when the button is pressed.
            controlButton.setOnAction(new EventHandler<ActionEvent>()
            {
                @Override
                public void handle(ActionEvent actionEvent)
                {
// create an animation to hide sidebar.
                    final Animation hideSidebar = new Transition()
                    {
                        {
                            setCycleDuration(Duration.millis(250));
                        }

                        @Override
                        protected void interpolate(double frac)
                        {
                            final double curWidth = expandedWidth * (1.0 - frac);
                            setPrefWidth(curWidth);
                            setTranslateX(-expandedWidth + curWidth);
                        }
                    };
                    hideSidebar.onFinishedProperty().set(new EventHandler<ActionEvent>()
                    {
                        @Override
                        public void handle(ActionEvent actionEvent)
                        {
                            setVisible(false);
                            controlButton.setText("Show");
                            controlButton.getStyleClass().remove("hide-left");
                            controlButton.getStyleClass().add("show-right");
                        }
                    });
// create an animation to show a sidebar.
                    final Animation showSidebar = new Transition()
                    {
                        {
                            setCycleDuration(Duration.millis(250));
                        }

                        @Override
                        protected void interpolate(double frac)
                        {
                            final double curWidth = expandedWidth * frac;
                            setPrefWidth(curWidth);
                            setTranslateX(-expandedWidth + curWidth);
                        }
                    };
                    showSidebar.onFinishedProperty().set(new EventHandler<ActionEvent>()
                    {
                        @Override
                        public void handle(ActionEvent actionEvent)
                        {
                            controlButton.setText("Collapse");
                            controlButton.getStyleClass().add("hide-left");
                            controlButton.getStyleClass().remove("show-right");
                        }
                    });
                    if (showSidebar.statusProperty().get() == Animation.Status.STOPPED && hideSidebar.statusProperty().get() == Animation.Status.STOPPED)
                    {
                        if (isVisible())
                        {
                            hideSidebar.play();
                        }
                        else
                        {
                            setVisible(true);
                            showSidebar.play();
                        }
                    }
                }
            });
        }
    }
}

如何在舞台底部显示折叠菜单?

还有一种方法可以在加载舞台时显示已关闭的面板。

1 个答案:

答案 0 :(得分:1)

使用您的代码:

import javafx.animation.Animation;
import javafx.animation.Transition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

public class MainApp extends Application
{
    BorderPane bp = new BorderPane();

    public static void main(String[] args) throws Exception
    {
        launch(args);
    }

    @Override
    public void start(final Stage stage) throws Exception
    {
        stage.setTitle("Slide out YouTube demo");

// create a WebView to show to the right of the SideBar.
        bp.setStyle("-fx-background-color: #2f4f4f;");
        bp.setPrefSize(800, 600);

// create a sidebar with some content in it.
        final Pane lyricPane = createSidebarContent();
        SideBar sidebar = new SideBar(250, lyricPane);
        VBox.setVgrow(lyricPane, Priority.ALWAYS);

// layout the scene.
        final BorderPane layout = new BorderPane();

        StackPane st = new StackPane();
        st.getChildren().addAll(bp, sidebar.getControlButton());
        st.setAlignment(Pos.TOP_LEFT);

        VBox vb = new VBox(10);
        vb.getChildren().addAll(st);

        layout.setBottom(sidebar);
        layout.setCenter(vb);

// show the scene.
        Scene scene = new Scene(layout);
        //scene.getStylesheets().add(getClass().getResource("/styles/slideout.css").toExternalForm());
        stage.setScene(scene);
        stage.show();
    }

    private BorderPane createSidebarContent()
    {
// create some content to put in the sidebar.
        final Button changeLyric = new Button("New Song");
        changeLyric.getStyleClass().add("change-lyric");
        changeLyric.setMaxWidth(Double.MAX_VALUE);
        changeLyric.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent actionEvent)
            {
                System.out.println("Some action");
            }
        });
        changeLyric.fire();
        final BorderPane lyricPane = new BorderPane();
        lyricPane.setTop(changeLyric);
        return lyricPane;
    }

    /**
     * Animates a node on and off screen to the left.
     */
    class SideBar extends VBox
    {
        /**
         * @return a control button to hide and show the sidebar
         */
        public Button getControlButton()
        {
            return controlButton;
        }
        private final Button controlButton;

        /**
         * creates a sidebar containing a vertical alignment of the given nodes
         */
        SideBar(final double expandedWidth, Node... nodes)
        {
            getStyleClass().add("sidebar");
            this.setPrefWidth(expandedWidth);
            this.setMinWidth(0);

// create a bar to hide and show.
            setAlignment(Pos.CENTER);
            getChildren().addAll(nodes);

// create a button to hide and show the sidebar.
            controlButton = new Button("Collapse");
            controlButton.getStyleClass().add("hide-left");

// apply the animations when the button is pressed.
            controlButton.setOnAction(new EventHandler<ActionEvent>()
            {
                @Override
                public void handle(ActionEvent actionEvent)
                {
// create an animation to hide sidebar.
                    final Animation hideSidebar = new Transition()
                    {
                        {
                            setCycleDuration(Duration.millis(250));
                        }

                        @Override
                        protected void interpolate(double frac)
                        {
                            final double curWidth = expandedWidth * (1.0 - frac);
                            setPrefHeight(curWidth);
                            setTranslateY(-expandedWidth + curWidth);
                        }
                    };
                    hideSidebar.onFinishedProperty().set(new EventHandler<ActionEvent>()
                    {
                        @Override
                        public void handle(ActionEvent actionEvent)
                        {
                            setVisible(false);
                            controlButton.setText("Show");
                            controlButton.getStyleClass().remove("hide-left");
                            controlButton.getStyleClass().add("show-right");
                        }
                    });
// create an animation to show a sidebar.
                    final Animation showSidebar = new Transition()
                    {
                        {
                            setCycleDuration(Duration.millis(250));
                        }

                        @Override
                        protected void interpolate(double frac)
                        {
                            final double curWidth = expandedWidth * frac;
                            setPrefHeight(curWidth);
                            setTranslateY(-expandedWidth + curWidth);
                        }
                    };
                    showSidebar.onFinishedProperty().set(new EventHandler<ActionEvent>()
                    {
                        @Override
                        public void handle(ActionEvent actionEvent)
                        {
                            controlButton.setText("Collapse");
                            controlButton.getStyleClass().add("hide-left");
                            controlButton.getStyleClass().remove("show-right");
                        }
                    });
                    if (showSidebar.statusProperty().get() == Animation.Status.STOPPED && hideSidebar.statusProperty().get() == Animation.Status.STOPPED)
                    {
                        if (isVisible())
                        {
                            hideSidebar.play();
                        }
                        else
                        {
                            setVisible(true);
                            showSidebar.play();
                        }
                    }
                }
            });
        }
    }
}

这使它扩大&#39; up&#39;单击按钮时。第一次点击它有点跳跃,我正在工作,所以我不能非常广泛地调试它,但基本上你只需在边框窗格的底部设置自定义侧边栏对象,然后将所有.width和.X方法切换为.height和.Y方法。通过一些调整,我相信你也可以让它扩展&#39; down&#39;代替。