java JavaFX:创建垂直菜单功能区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28717343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:03:51  来源:igfitidea点击:

JavaFX: create a vertical menu ribbon

javajavafxfxml

提问by usertest

An example of what I am trying to accomplish here is: when you open an office Word 2013 file and you click file, in the left side it shows a list {Info, New, Open...}.

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

Are there any JavaFX componentslike that? I am looking for a kind of List of (something) whose items are aligned vertically and you can click on to do something (in my case change the view on the right exactly like Word).

有没有这样的 JavaFX组件?我正在寻找一种(某物)列表,其项目垂直对齐,您可以单击以执行某些操作(在我的情况下,就像 Word 一样更改右侧的视图)。

Word Menu Ribbon

Word 菜单功能区

回答by Surprised Coconut

You can easily reproduce the Word like menu with a VBox and buttons with custom CSS styles. Here is a quick and dirty example that shows a possible solution.

您可以使用 VBox 和具有自定义 CSS 样式的按钮轻松重现类似 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);
    }

}

Plus the styles.css.

加上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.

最初的场景是。

The same scene after clicking the File button. I used a FadeTransition here because it is simple. But of course you can try to reproduce the same animation as in Word.

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

The scene after clicking the File button.

单击文件按钮后的场景。

回答by James_D

Here's a quick example using a styled MenuButton.

这是一个使用样式的快速示例MenuButton

import java.util.stream.Collectors;
import java.util.stream.Stream;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class WordLikeMenuButton extends Application {

    @Override
    public void start(Stage primaryStage) {
        MenuButton menuButton = new MenuButton();
        menuButton.getItems().addAll(
                Stream.of("Info", "New", "Open", "Save", "Save As", "Print", "Share", "Export", "Close")
                    .map(MenuItem::new).collect(Collectors.toList()));
        BorderPane root = new BorderPane(null, menuButton, null, null, null);
        Scene scene = new Scene(root, 350, 75);
        scene.getStylesheets().add("word-like-menu-button.css");
        primaryStage.setScene(scene);
        primaryStage.show();
    }


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

word-like-menu-button.css:

word-like-menu-button.css:

.menu-button, .menu-button .menu-item, .menu-button .context-menu {
    -fx-background-color: #28559c;  
}
.menu-button .menu-item:hover {
    -fx-background-color: #3b6bb7 ;
}
.menu-button .menu-item .label {
    -fx-text-fill: white ;
}
.menu-button > .arrow-button {
    -fx-background-color: white, #28559c ;
    -fx-background-insets: 1, 3 ;
    -fx-background-radius: 16, 16 ;
    -fx-padding: 8 ;

}
.menu-button > .arrow-button > .arrow {
    -fx-background-color: white ;
    /*-fx-background-insets: 0, 2 ;
    -fx-background-radius: 12, 12 ;*/
    -fx-padding: 8 ; 
    -fx-shape: "M0 6 l-6 -6 l0 -2 l6 -6 l2 0 l-6 6 l12 0 l0 2 l-12 0 l6 6 z";

}

This gives

这给

enter image description here

在此处输入图片说明

回答by Sebastian Suchanowski

It would be much better to use JFXTabPaneand customize it a little bit. As for the back arrow, you can do what I did with logout button in this tutorial http://synappse.co/blog/vertical-stateful-jfxtabpane-with-icons-in-javafx/

JFXTabPane稍微使用和定制它会更好。至于后退箭头,您可以在本教程中使用注销按钮执行我所做的操作http://synappse.co/blog/vertical-stateful-jfxtabpane-with-icons-in-javafx/