JavaFX SegmentedButton-按钮全宽
我得到了一个JavaFX SegmentedButton-按钮全宽,java,javafx,fxml,controlsfx,Java,Javafx,Fxml,Controlsfx,我得到了一个分段按钮,其中包含3个“仅字形”切换按钮,如下所示: <SegmentedButton maxWidth="Infinity" prefWidth="Infinity"> <buttons> <fx:define> <ToggleGroup fx:id="albumViewToggleGroup"/> </fx:define> <Toggl
分段按钮
,其中包含3个“仅字形”切换按钮
,如下所示:
<SegmentedButton maxWidth="Infinity" prefWidth="Infinity">
<buttons>
<fx:define>
<ToggleGroup fx:id="albumViewToggleGroup"/>
</fx:define>
<ToggleButton maxWidth="Infinity" fx:id="tagCloudToggle" mnemonicParsing="false" selected="true" toggleGroup="$albumViewToggleGroup">
<graphic>
<Glyph fontFamily="FontAwesome" icon="TAGS"></Glyph>
</graphic>
</ToggleButton>
<ToggleButton maxWidth="Infinity" fx:id="gridFlowToggle" mnemonicParsing="false" toggleGroup="$albumViewToggleGroup" >
<graphic>
<Glyph fontFamily="FontAwesome" icon="TH"></Glyph>
</graphic>
</ToggleButton>
<ToggleButton maxWidth="Infinity" fx:id="coverFlowToggle" mnemonicParsing="false" toggleGroup="$albumViewToggleGroup">
<graphic>
<Glyph fontFamily="FontAwesome" icon="ELLIPSIS_H"></Glyph>
</graphic>
<VBox.margin>
<Insets top="10.0"/>
</VBox.margin>
</ToggleButton>
</buttons>
</SegmentedButton>
SegmenedButton使用全宽(由红线表示),但ToggleButton不使用。我通过设置背景色来检查这一点
我希望ToggleButtons被拉伸,使它们每个都是SegmentedButton宽度的1/3。我怎样才能做到这一点 我怀疑你在这个问题上是否还需要帮助,但对于像我这样通过谷歌搜索问题并来到这里的人来说,我是如何解决这个问题的:
button1.prefWidthProperty().bind(segmentedButtons.widthProperty().divide(2));
button2.prefWidthProperty().bind(segmentedButtons.widthProperty().divide(2));
segmentedButtons.getButtons().addAll(button1, button2);
segmentedButtons.setMaxWidth(Double.MAX_VALUE);
因此,基本上,对于添加到SegmentedButton的每个ToggleButton,您将首选宽度绑定到SegmentedButton的实际宽度除以按钮数量。因为它是绑定的,所以在调整窗口大小时宽度会调整,所以在创建时只需执行一次。如果您愿意,您可以用其他方法划分宽度,使一些按钮变大一些变小。根据数学运算符,可以在fxml绑定中使用
因此,可以使用这样的方法:
<SegmentedButton fx:id="segButton" maxWidth="1.7976931348623157E308" >
<buttons>
<ToggleButton fx:id="option1" maxWidth="1.7976931348623157E308" prefWidth="${segButton.width / 4}" text="K3" />
<ToggleButton fx:id="option1" maxWidth="1.7976931348623157E308" prefWidth="${segButton.width / 4}" text="K2" />
<ToggleButton fx:id="option1" maxWidth="1.7976931348623157E308" prefWidth="${segButton.width / 4}" text="K1" />
<ToggleButton fx:id="option1" maxWidth="1.7976931348623157E308" prefWidth="${segButton.width / 4}" text="K1,1" />
</buttons>
</SegmentedButton>
可能不再需要了,但希望对那些最终在谷歌上搜索的人有用。我希望我不必编写实际的java代码来设计我的应用程序;)我知道你可以用几行java代码做任何事情。尽管整个JavaFXIMHO背后的真正概念应该是将样式和使用fxml文件分开。所以“代码隐藏”(我们在.NET中称之为“代码隐藏”)并不是我真正想要的解决方案。