简体   繁体   English

JavaFX:自定义网格窗格中的按钮宽度

[英]JavaFX: Button's width in custom gridpane

I would like to have buttons with equal(maximum) width in a gridpane.我想在网格窗格中具有相等(最大)宽度的按钮。

When I'm trying to set it directly in FXML - it works perfectly.当我试图直接在 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>

But when I wanted to separate the whole grid into a custom control, the buttons stopped to fill the available width.但是当我想将整个网格分成一个自定义控件时,按钮停止填充可用宽度。

Here's a FXML:这是一个 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>

And an extension:还有一个扩展:

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);
    }
}

Am I missing something?我错过了什么吗?

You are using maxWidth property getter instead of the method setMaxWidth (setter).您正在使用 maxWidth 属性 getter 而不是 setMaxWidth (setter) 方法。

Please see the documentation of Button here请在此处查看按钮的文档

public final double maxWidth(double height) 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 属性覆盖最大宽度,否则返回来自 computeMaxWidth(forHeight) 的值。

Replace the two lines with maxWidth(...) by these ones:用 maxWidth(...) 替换这两行:

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

This can be achieved inside scene builder also...这也可以在场景构建器中实现......

跟随黑色箭头

I required this, while creating a calculator, with the buttons inside gridpane.我需要这个,同时创建一个计算器,带有网格面板内的按钮。

Happy Coding (:快乐编码(:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM