Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javaFX中创建包含左、中、右部分的工具栏?_Java_Javafx_Alignment_Toolbar - Fatal编程技术网

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

如何在javaFX中创建包含左、中、右部分的工具栏?,java,javafx,alignment,toolbar,Java,Javafx,Alignment,Toolbar,我正试图在javafx中创建一个自定义工具栏。此工具栏应能够在其曲面的中心、左侧和右侧(三个部分)显示控件。 问题是我不知道如何做到这一点。我读了很多关于这个问题的提示,但它们对我不起作用,或者我做错了什么 无论如何,我写了几个方法,它们代表了实现工具栏的不同方式,但没有一个能正常工作。 以下是我的尝试: 使用HBox Hgrow属性作为spring。没用 public ToolBar createToolBar() { ToolBar toolBar = new ToolBar();

我正试图在javafx中创建一个自定义工具栏。此工具栏应能够在其曲面的中心、左侧和右侧(三个部分)显示控件。 问题是我不知道如何做到这一点。我读了很多关于这个问题的提示,但它们对我不起作用,或者我做错了什么

无论如何,我写了几个方法,它们代表了实现工具栏的不同方式,但没有一个能正常工作。 以下是我的尝试:

  • 使用HBox Hgrow属性作为spring。没用

    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.它适用于左侧和右侧部分,但如何定义中间部分

        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;
        }
    
  • 这种方法对布局很有效,但我无法从左侧和中间部分监听事件,因为右侧部分包含了这些事件(原因是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;
    }
    
  • 我在该代码块中调用的上述方法:

       @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();
        }
    

    我将非常感谢您在这方面提供的任何帮助。

    用于工具栏部分对齐的Spring方法对我来说很好

    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(至少在Java 8的标准modena.css样式表中)已经在内部实现为HBox容器,因此您可以使用该方法调整放置在工具栏中的项目的内部布局约束

    本例中的左间隔和右间隔是作为空白窗格实现的,它只会增长和收缩以适应可用空间

    如果您想要一个固定大小的垫片,而不是HBox.setHgrow,您可以使用以下内容:

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

    只需使用HBox而不是StackPane。StackPane将节点放在彼此的顶部

    另一个选项是使用FX工具栏,类似于前面提到的:


    这两种尝试都应该足够灵活,以满足您的需要。它们的不同之处在于对布局的控制程度以及可以从标准工具栏继承的功能数量

    将FXML与@jewelsea answer结合使用

    <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>
    
    
    
    看一看。您可以用各自的控件替换文章中的ImageView、Image和VBox。感谢提供有用的示例!我尝试了扩展fx工具栏的方法,效果非常好:)谢谢。我将您的解决方案改编为FXML。见下面我的答案