JavaFX SegmentedButton - 全宽按钮

标签 java javafx fxml controlsfx

我有一个 SegmentedButton,它包含 3 个“仅字形”ToggleButtons,如下所示:

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

SegmenedtButton 占据了整个宽度(用红线表示),但 ToggleButton 却没有。我通过设置背景颜色来检查这一点。

enter image description here

我希望 ToggleButton 被拉伸(stretch),以便它们每个都是 SegmentedButton 宽度的 1/3。我怎样才能做到这一点?

最佳答案

根据documentatiom可以在 fxml 绑定(bind)中使用数学运算符。

所以可以使用这样的东西:

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

可能不再需要,但希望对最终使用 google 搜索此内容的任何人有用。

关于JavaFX SegmentedButton - 全宽按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31155547/

相关文章:

java - 将 Java 程序做成独立的 exe(不用 JVM 运行)

java - 避免Java因证书错误而出现安全警告

java - 如何在 JavaFX 中传递参数

java - 什么是 Java 中的反向引用?

java - 在 android 中解析此类 url 的最佳方法是什么

java 。如何使用私有(private)构造函数覆盖类中的方法

css - 垂直对齐 CheckboxTableCell 的复选框

java - 客户端麦克风笔记分析

java - 在 JavaFx 中重新启动应用程序

javafx-2 - 阻塞父阶段直到子阶段关闭