JavaFX:创建垂直菜单功能区

标签 java javafx fxml

我在这里尝试完成的一个示例是:当您打开 Office Word 2013 文件并单击文件时,它会在左侧显示一个列表 {Info, New, Open...}。

是否有类似的 JavaFX 组件?我正在寻找一种(某物)的列表,其项目垂直对齐,您可以单击以执行某些操作(在我的例子中,完全像 Word 一样更改右侧的 View )。

Word Menu Ribbon

最佳答案

您可以使用带有自定义 CSS 样式的 VBox 和按钮轻松重现类似 Word 的菜单。这是一个显示可能解决方案的快速粗略示例。

public class Jfxdemos extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("File");

        final StackPane root = new StackPane();
        AnchorPane editorRoot = new AnchorPane();
        editorRoot.getChildren().add(btn);
        root.getChildren().add(editorRoot);

        Scene scene = new Scene(root, 300, 250);
        scene.getStylesheets().add("/jfxdemos/styles.css");

        primaryStage.setScene(scene);
        primaryStage.show();

        HBox fileRoot = new HBox();
        VBox menu = new VBox();
        menu.setStyle("-fx-background-color: blue;");
        menu.setFillWidth(true);
        Button backBtn = new Button("Left Arrow");
        backBtn.setPrefWidth(100);
        backBtn.getStyleClass().add("custom-menu-button");
        backBtn.setOnAction(new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent event) {
                FadeTransition hideFileRootTransition = new FadeTransition(Duration.millis(500), fileRoot);
                hideFileRootTransition.setFromValue(1.0);
                hideFileRootTransition.setToValue(0.0);

                FadeTransition showEditorRootTransition = new FadeTransition(Duration.millis(500), editorRoot);
                showEditorRootTransition.setFromValue(0.0);
                showEditorRootTransition.setToValue(1.0);

                showEditorRootTransition.play();
                hideFileRootTransition.play();
                root.getChildren().remove(fileRoot);
            }
        });
        Button infoBtn = new Button("Info");
        infoBtn.setPrefWidth(100);
        infoBtn.getStyleClass().add("custom-menu-button");
        Button newBtn = new Button("New");
        newBtn.setPrefWidth(100);
        newBtn.getStyleClass().add("custom-menu-button");
        Button openBtn = new Button("Open");
        openBtn.setPrefWidth(100);
        openBtn.getStyleClass().add("custom-menu-button");
        menu.getChildren().addAll(backBtn,infoBtn, newBtn, openBtn);
        VBox.setVgrow(infoBtn, Priority.ALWAYS);
        fileRoot.getChildren().add(menu);

        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                root.getChildren().add(fileRoot);
                FadeTransition hideEditorRootTransition = new FadeTransition(Duration.millis(500), editorRoot);
                hideEditorRootTransition.setFromValue(1.0);
                hideEditorRootTransition.setToValue(0.0);

                FadeTransition showFileRootTransition = new FadeTransition(Duration.millis(500), fileRoot);
                showFileRootTransition.setFromValue(0.0);
                showFileRootTransition.setToValue(1.0);
                hideEditorRootTransition.play();
                showFileRootTransition.play();
            }
        });

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

加上 styles.css。

.custom-menu-button {
    -fx-background-color: blue;
    -fx-text-fill: white;
    -fx-border: none; 
}

.custom-menu-button:hover {
    -fx-background-color: lightblue;
}

The initial scene is.

单击"file"按钮后的同一场景。我在这里使用了 FadeTransition,因为它很简单。但您当然可以尝试重现与 Word 中相同的动画。

The scene after clicking the File button.

关于JavaFX:创建垂直菜单功能区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28717343/

相关文章:

javafx-2 - 如何在运行时使用Button Click更改子fxml gui部件

java - Java 的 PreparedStatement 是如何工作的?

java - Spring获取没有注释的entityManager

Java - 将十六进制转换为 IEEE-754 64 位 float - double

java - 无法在 JavaFX UI 中重用文本字段

controller - JavaFX8 FXML Controller 注入(inject)

java - 是否可以让单个文档监听器在 Swing 中监听多个文本字段

css - JavaFx 如何在 CSS 中使用 java 生成的 RGB 颜色

java - 根据行项目创建CellFactory

java - 如何在创建的原始窗口内的 Controller 之间进行转换