【问题标题】:JavaFX: Button's width in custom gridpaneJavaFX:自定义网格窗格中按钮的宽度
【发布时间】:2015-03-08 05:17:36
【问题描述】:

我希望在网格窗格中具有相等(最大)宽度的按钮。

当我尝试直接在 FXML 中设置它时 - 它工作得很好。

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.*?>
<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
            fx:controller="TestController">
    <children>
        <GridPane AnchorPane.leftAnchor="0" AnchorPane.rightAnchor="0" AnchorPane.bottomAnchor="0"
                  AnchorPane.topAnchor="0">
            <columnConstraints>
                <ColumnConstraints hgrow="SOMETIMES" percentWidth="50.0"/>
                <ColumnConstraints hgrow="SOMETIMES" percentWidth="50.0"/>
            </columnConstraints>
            <children>
                <Button maxWidth="Infinity" text="Button1" GridPane.columnIndex="0"/>
                <Button maxWidth="Infinity" text="Button2" GridPane.columnIndex="1"/>
            </children>

        </GridPane>
    </children>
</AnchorPane>

但是当我想将整个网格分成一个自定义控件时,按钮会停止以填充可用宽度。

这是一个 FXML:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>
<?import CustomGridPane?>
<AnchorPane xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
            fx:controller="TestController">
    <children>
        <CustomGridPane AnchorPane.leftAnchor="0" AnchorPane.rightAnchor="0" AnchorPane.bottomAnchor="0" AnchorPane.topAnchor="0">
        </CustomGridPane>
    </children>
</AnchorPane>

还有一个扩展:

public class CustomGridPane extends GridPane {

    private Button button1 = new Button("button1");
    private Button button2 = new Button("button2");

    public CustomGridPane() {
        super();
        button1.maxWidth(Double.MAX_VALUE);
        button2.maxWidth(Double.MAX_VALUE);

        getColumnConstraints().add(new ColumnConstraints());
        getColumnConstraints().add(new ColumnConstraints());
        getColumnConstraints().get(0).setPercentWidth(50);
        getColumnConstraints().get(0).setHgrow(Priority.SOMETIMES);
        getColumnConstraints().get(1).setPercentWidth(50);
        getColumnConstraints().get(1).setHgrow(Priority.SOMETIMES);

        add(button1, 0, 0);
        add(button2, 1, 0);
    }
}

我错过了什么吗?

【问题讨论】:

    标签: button javafx width custom-controls gridpane


    【解决方案1】:

    您正在使用 maxWidth 属性 getter 而不是方法 setMaxWidth (setter)。

    请看Buttonhere的文档

    public final double maxWidth(double height)

    在布局期间调用以确定此节点的最大宽度。从 computeMaxWidth(forHeight) 返回值,除非应用程序通过设置 maxWidth 属性覆盖了最大宽度。

    用 maxWidth(...) 替换这两行:

        button1.setMaxWidth(Double.MAX_VALUE);
        button2.setMaxWidth(Double.MAX_VALUE);
    

    【讨论】:

    • 是的,很明显。我不敢相信我盯着它看了这么久并没有注意到它。谢谢。
    【解决方案2】:

    这也可以在场景构建器中实现...

    我在创建计算器时需要此功能,并在网格窗格中使用按钮。

    快乐编码 (:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-27
      • 1970-01-01
      • 2015-05-27
      • 2020-03-01
      • 2017-02-21
      • 2012-09-24
      相关资源
      最近更新 更多