简体   繁体   English

"如何在 javaFX 中创建具有左、中、右部分的工具栏?"

[英]How to create toolbar with left, center and right sections in javaFX?

I'm trying to create a customized toolbar in javafx.我正在尝试在 javafx 中创建自定义工具栏。 This toolbar should be able to display controls in the center, in the left side and in the right side (three sections) of its surface.该工具栏应该能够在其表面的中心、左侧和右侧(三个部分)显示控件。 The problem is that I have no idea to achive this.问题是我不知道要做到这一点。 I read many hints related to this problem, but they dont work for me or I'm doing something wrong...我阅读了许多与此问题相关的提示,但它们对我不起作用或者我做错了什么......

Anyway I wrote several methods, which represent different ways to implement my toolbar but no of them works properly.无论如何,我写了几个方法,它们代表了实现我的工具栏的不同方法,但它们都不能正常工作。 Here you have my attempts:在这里你有我的尝试:

  1. Using HBox Hgrow property as spring.使用 HBox Hgrow 属性作为弹簧。 Didn't work.没用。

     public ToolBar createToolBar() { ToolBar toolBar = new ToolBar(); Pane emptyPane = new Pane(); HBox spring = new HBox(emptyPane); spring.setHgrow(emptyPane, Priority.ALWAYS); toolBar.getItems().addAll(spring, new Label("LABEL")); return toolBar; }

2.It works for the left and right section, but how to define the center one? 2.左右部分都可以,中间部分怎么定义?

    public AnchorPane createToolBar2()
    {
        AnchorPane toolBar = new AnchorPane();
        Label leftLabel = new Label("left");
        Label rightLabel = new Label("right");
        toolBar.getChildren().addAll(leftLabel, rightLabel);
        toolBar.setLeftAnchor(leftLabel, 0.0);
        toolBar.setRightAnchor(rightLabel, 0.0);
        return toolBar;
    }
  1. This approach works well for the layout, but I cannot listen for events from left and center section because those are covered by the right section (StackPane is the reason), so this solution is also useless.这种方法适用于布局,但我无法侦听左侧和中心部分的事件,因为右侧部分覆盖了这些事件(原因是 StackPane),因此该解决方案也无用。

     public StackPane createToolBar3() { StackPane toolBar = new StackPane(); Button left = new Button("left button"); Button right = new Button("right button"); Button center = new Button("center button"); HBox leftSection = new HBox(left); leftSection.setAlignment(Pos.CENTER_LEFT); HBox centerSection = new HBox(center); centerSection.setAlignment(Pos.CENTER); HBox rightSection = new HBox(right); rightSection.setAlignment(Pos.CENTER_RIGHT); toolBar.getChildren().addAll(leftSection, centerSection, rightSection); left.setOnAction(event -> System.out.println("left")); right.setOnAction(event -> System.out.println("right")); center.setOnAction(event -> System.out.println("center")); return toolBar; }

Above methods I'm invoking in that code block:我在该代码块中调用的上述方法:

   @Override
    public void start(Stage stage) {

        BorderPane borderPane = new BorderPane();
        borderPane.setPrefWidth(500);
        borderPane.setPrefHeight(300);
        borderPane.setTop(createToolBar4());
        stage.setScene(new Scene(borderPane));
        stage.show();
    }

I will be grateful for any help in that matter.我将不胜感激在这件事上的任何帮助。

Spring method for ToolBar section alignment works fine for me.工具栏部分对齐的弹簧方法对我来说很好。

弹簧工具

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class SectionalToolbar extends Application {
    @Override
    public void start(Stage stage) throws Exception {
        final Pane leftSpacer = new Pane();
        HBox.setHgrow(
                leftSpacer,
                Priority.SOMETIMES
        );

        final Pane rightSpacer = new Pane();
        HBox.setHgrow(
                rightSpacer,
                Priority.SOMETIMES
        );

        final ToolBar toolBar = new ToolBar(
                new Button("Good"),
                new Button("Boys"),
                leftSpacer,
                new Button("Deserve"),
                new Button("Fruit"),
                rightSpacer,
                new Button("Always")
        );
        toolBar.setPrefWidth(400);

        BorderPane layout = new BorderPane();
        layout.setTop(toolBar);

        stage.setScene(
                new Scene(
                        layout
                )
        );
        stage.show();
    }
    public static void main(String[] args) { launch(); }
}  

A ToolBar (at least in the standard modena.css stylesheet for Java 8), is already internally implemented as a HBox container, so you can just use the HBox.setHgrow method to adjust the internal layout constraints of items you place in a toolbar. ToolBar (至少在 Java 8 的标准 modena.css 样式表中)已经在内部实现为 HBox 容器,因此您只需使用HBox.setHgrow方法来调整放置在工具栏中的项目的内部布局约束。

The left and right spacers in this example are implemented as blank panes which will just grow and shrink to fit available space.此示例中的左右分隔符被实现为空白窗格,它们只会增长和缩小以适应可用空间。

If you wanted a spacer which was a fixed size, instead of HBox.setHgrow, you could use something like:如果你想要一个固定大小的垫片,而不是 HBox.setHgrow,你可以使用类似的东西:

leftSpacer.setPrefSize(20); 
leftSpacer.setMinSize(HBox.USE_PREF_SIZE); 
leftSpacer.setMaxSize(HBox.USE_PREF_SIZE);

Using FXML in conjunction with @jewelsea answer.将 FXML 与 @jewelsea 答案结合使用。

<ToolBar prefHeight="40.0" prefWidth="200.0">
    <items>
        <Button mnemonicParsing="false" text="Good" />
        <Button mnemonicParsing="false" text="Boys" />
        <Pane prefWidth="20.0" HBox.hgrow="SOMETIMES" />
        <Button mnemonicParsing="false" text="Deserve" />
        <Button mnemonicParsing="false" text="Fruit" />
        <Pane prefWidth="20.0" HBox.hgrow="SOMETIMES" />
        <Button mnemonicParsing="false" text="Always" />
        <Pane prefWidth="20.0" HBox.hgrow="SOMETIMES" />
    </items>
</ToolBar>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM