gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 00:32:10 25 4
gpt4 key购买 nike

我希望在网格 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com