简体   繁体   English

您如何指定要使工具栏中的元素居中?

[英]How do you specify that you want your elements in a ToolBar to be centered?

For some reason, JavaFX's ToolBar only proposes two options for items alignment: LEFT_TO_RIGHT and RIGHT_TO_LEFT . 出于某种原因,JavaFX的ToolBar仅建议两个用于项目对齐的选项: LEFT_TO_RIGHTRIGHT_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... 有趣的是,如果这是RIGHT_TO_LEFT ,则必须以相反的顺序指定您的商品, 以便它们自然显示。

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: 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>

the 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);
    }
}

But this is what it gives: 但这就是它的作用:

在此处输入图片说明

FOUND. 找到。

In fact, the code is pretty "simple". 实际上,代码非常“简单”。 Kind of, since this really should be in the ToolBar for starters, but here goes... 有点,因为对于初学者来说,这确实应该ToolBar栏中,但是这里...

The FXML is now as follows: 现在,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>

And the code is simply: 代码很简单:

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

    @FXML
    protected HBox hbox;

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

Well there is this little hack to produce a dynamic spacer: 好了,这个小技巧可以产生动态间隔:

final Region spacer = new Region();
HBox.setHgrow(spacer, Priority.SOMETIMES);
spacer.setMinWidth(Region.USE_PREF_SIZE);

If you put one as first item and another one as last item on the ToolBar your items inbetween should be centered. 如果将一个作为第一项,将另一个作为最后一项放在ToolBar上,则中间的项应居中。

Just keep an eye on the width of the Toolbar and apply the new value to the HBox. 只需注意工具栏的宽度,然后将新值应用于HBox。 This is necessary in order to monitor the size of the Stage 为了监视舞台的大小,这是必需的

toolbar.widthProperty().addListener((obs, oldVal, newVal) -> {  
    hbox.setPrefWidth(newVal.doubleValue());
});

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

相关问题 你如何在你的<web-app> web.xml 中的标签?</web-app> - How do you specify the root context in your <web-app> tags in web.xml? 获得不需要的弹出窗口“您尚未保存更改。如果关闭此流程,则您的更改将丢失。您要继续吗?” - Getting unwanted popup "You have not saved your changes. If you close this flow, then your changes will be lost. Do you want to continue?" 如何将PowerMock添加到项目中? - How do you add PowerMock to your project? 如何垂直扩展ActiveMQ? - How do you scale your ActiveMQ vertically? 在Spring中,如何指定BindingResult实现 - In Spring, how do you specify the BindingResult implementation 你如何在 Java 中指定字节文字? - How do you specify a byte literal in Java? 如何使用spring指定可以动态更改的数据源 - How do you specify a datasource with spring that you can change dynamically 如何在Java中绘制一个垂直居中的字符串? - How do you draw a string centered vertically in Java? 为什么要在Spring Boot中使用@PropertySource时需要指定@Configuration? - Why do you need to specify @Configuration when you want to use @PropertySource in Spring Boot? Javamail API - 如何将setFrom更改为您想要的任何内容? - Javamail API - How do you change setFrom to whatever you want?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM