button - JavaFX:自定义网格 Pane 中按钮的宽度

标签 button javafx width custom-controls gridpane

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

当我尝试直接在 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);
    }
}

我错过了什么吗?

最佳答案

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

请参阅按钮的文档here

public final double maxWidth(double height)

Called during layout to determine the maximum width for this node. Returns the value from computeMaxWidth(forHeight) unless the application overrode the maximum width by setting the maxWidth property.

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

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

关于button - JavaFX:自定义网格 Pane 中按钮的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27868127/

相关文章:

javafx双向更改通知不起作用?

html - 如何启用 HTML TextArea 自动调整宽度?

java - 为什么 JavaFX 舞台/窗口/应用程序在更改场景后不刷新?

java - 如何停止在java swing中执行的 Action

ios - 点击时如何反复更改按钮?

android - android开发的继承方法崩溃

使用 Color.web 时 JavaFX 找不到符号

html - 棘手的 CSS 布局

javascript - svg 附加文本元素 - 给我错误的宽度

twitter-bootstrap - Bootstrap 导航栏右对齐按钮