如何指定您希望工具栏中的元素居中? [英] How do you specify that you want your elements in a ToolBar to be centered?

查看:308
本文介绍了如何指定您希望工具栏中的元素居中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,JavaFX的 ToolBar 仅为项目对齐提出了两个选项: LEFT_TO_RIGHT RIGHT_TO_LEFT

For some reason, JavaFX's ToolBar only proposes two options for items alignment: LEFT_TO_RIGHT and RIGHT_TO_LEFT.

有趣的是,如果这是 RIGHT_TO_LEFT 你必须指定逆序中的项目,以便它们自然显示...

And funnily enough, if this is RIGHT_TO_LEFT you have to specify your items in reverse order so that they show up naturally...

但是我没有看到任何选项来对齐中心。你是如何实现这一目标的?或者我必须使用除工具栏以外的其他东西吗?

However I don't see any option for aligning elements in the center. How do you achieve that? Or must I use something else than a toolbar?

编辑:这是当前代码...不幸的是这不是'工作:(

edit: here is the current code... Unfortunately this doesn't work :(

FXML:

<BorderPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
    fx:controller="com.github.fge.grappa.debugger.csvtrace.tabs.linechart.LineChartTabDisplay">
    <top>
        <ToolBar fx:id="toolbar">
            <HBox fx:id="hbox" alignment="CENTER">
                <Region fx:id="leftSpacer"/>
                <Button text="Refresh"/>
                <ProgressBar visible="false"/>
                <Label text="Layout testing"/>
                <Region fx:id="rightSpacer"/>
            </HBox>
        </ToolBar>
    </top>
</BorderPane>

class:

public class LineChartTabDisplay
    extends JavafxDisplay<LineChartTabPresenter>
{

    public Region leftSpacer;
    public Region rightSpacer;
    public ToolBar toolbar;
    public HBox hbox;

    @Override
    public void init()
    {
        HBox.setHgrow(leftSpacer, Priority.SOMETIMES);
        leftSpacer.setMinWidth(Region.USE_PREF_SIZE);
        HBox.setHgrow(rightSpacer, Priority.SOMETIMES);
        rightSpacer.setMinWidth(Region.USE_PREF_SIZE);
    }
}

但这就是它所给出的:

推荐答案

找到。

事实上,代码非常简单。有点,因为这真的 应该在 ToolBar 中作为初学者,但这里有...

In fact, the code is pretty "simple". Kind of, since this really should be in the ToolBar for starters, but here goes...

FXML现在如下:

<BorderPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
    fx:controller="com.github.fge.grappa.debugger.csvtrace.tabs.linechart.LineChartTabDisplay">
    <top>
        <ToolBar fx:id="toolbar">
            <HBox fx:id="hbox" alignment="CENTER" spacing="5.0">
                <Button text="Refresh"/>
                <Label text="Layout testing"/>
            </HBox>
        </ToolBar>
    </top>
</BorderPane>

代码很简单:

public class LineChartTabDisplay
    extends JavafxDisplay<LineChartTabPresenter>
{
    @FXML
    protected ToolBar toolbar;

    @FXML
    protected HBox hbox;

    @Override
    public void init()
    {
        hbox.minWidthProperty().bind(toolbar.widthProperty());
    }
}

这篇关于如何指定您希望工具栏中的元素居中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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