如何在javaFX中创建左,中,右部分的工具栏? [英] How to create toolbar with left, center and right sections in javaFX?

查看:296
本文介绍了如何在javaFX中创建左,中,右部分的工具栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create a customized toolbar in 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. 使用HBox Hgrow属性作为弹簧。没用。

  1. Using HBox Hgrow property as spring. 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.它的工作原理对于左侧和右侧部分,但如何定义中心部分?

2.It works for the left and right section, but how to define the center one?

    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. 这方法适用于布局,但我无法监听左侧和中间部分的事件,因为这些事件由正确的部分覆盖(StackPane是原因),所以这个解决方案也没用。

  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.

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.

推荐答案

只需使用HBox而不是StackPane。 StackPane将节点放在彼此之上。

Simply use a HBox instead of a StackPane. A StackPane puts Nodes on top of each other.

查看第三次尝试的修改示例。

另一种选择是使用FX ToolBar,类似如何 jewelsea 已经提到:

Another option would be to use the FX ToolBar, similar to how jewelsea already mentioned:

使用工具栏查看示例。

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

Both attempts should be flexible enough for your needs. They differ in how much control you get over the layout and how many features you can inherit from the standard ToolBar.

这篇关于如何在javaFX中创建左,中,右部分的工具栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆