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

Qbi*_*iek 5 java javafx toolbar alignment

我正在尝试在javafx中创建自定义工具栏.此工具栏应该能够在其表面的中心,左侧和右侧(三个部分)显示控件.问题是我不知道这个.我读了很多与这个问题相关的提示,但是他们不适合我,或者我做错了什么......

无论如何,我写了几个方法,它们代表了实现我的工具栏的不同方法,但没有一个正常工作.在这里你有我的尝试:

  1. 使用HBox Hgrow属性作为弹簧.没工作.

    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;
    }
    
    Run Code Online (Sandbox Code Playgroud)

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;
    }
Run Code Online (Sandbox Code Playgroud)
  1. 这种方法适用于布局,但我无法监听左侧和中间部分的事件,因为这些事件由右侧部分覆盖(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;
    }
    
    Run Code Online (Sandbox Code Playgroud)

我正在该代码块中调用的上述方法:

   @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();
    }
Run Code Online (Sandbox Code Playgroud)

我将非常感谢你在这方面的任何帮助.

jew*_*sea 8

ToolBar部分对齐的Spring方法对我来说很好.

springtool

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(); }
}  
Run Code Online (Sandbox Code Playgroud)

一个工具栏(至少在标准modena.css样式表对Java 8),已在内部实现为HBox容器,所以你可以只使用HBox.setHgrow方法来调整您在工具栏中放置物品的内部布局约束.

此示例中的左侧和右侧间隔器实现为空白窗格,它们只会增长和缩小以适合可用空间.

如果你想要一个固定大小的垫片,而不是HBox.setHgrow,你可以使用类似的东西:

leftSpacer.setPrefSize(20); 
leftSpacer.setMinSize(HBox.USE_PREF_SIZE); 
leftSpacer.setMaxSize(HBox.USE_PREF_SIZE);
Run Code Online (Sandbox Code Playgroud)