[英]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_RIGHT
和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... 有趣的是,如果这是
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.