JavaFX 区域的布局问题
Layouting problems with JavaFX Region
我想编写一个新的 class 来扩展包含 StackPane 的区域。但是当我向它添加填充或边框等插图时,我遇到了麻烦。这是 class:
的简化示例
public class CustomPane extends Region
{
private ToggleButton testControl = new ToggleButton("just a test control");
private StackPane rootPane = new StackPane(testControl);
public CustomPane()
{
getChildren().add(rootPane);
setStyle("-fx-border-color: #257165; -fx-border-width: 10;");
}
}
这就是结果的样子:
如果我尝试通过调用
来移动 StackPane
rootPane.setLayoutX(10);
rootPane.setLayoutY(10);
那么该区域就会增长:
但我真的希望它看起来像这样:
(第三张图片是通过扩展 StackPane 而不是 Region 创建的,后者已经正确管理了布局内容。不幸的是,我必须扩展 Region,因为我想保持 getChildren()
受保护。)
好的,我尝试处理布局计算,但我没有想到。专家可以给我一些建议吗?
StackPane
将 insets
for layouting the (managed) children. Region
doesn't do this by default. Therefore you need to override the layoutChildren
与使用这些 insets
的东西一起使用,例如:
@Override
protected void layoutChildren() {
Insets insets = getInsets();
double top = insets.getTop(),
left = insets.getLeft(),
width = getWidth() - left - insets.getRight(),
height = getHeight() - top - insets.getBottom();
// layout all managed children (there's only rootPane in this case)
layoutInArea(rootPane,
left, top, // offset of layout area
width, height, // available size for content
0,
HPos.LEFT,
VPos.TOP);
}
我想编写一个新的 class 来扩展包含 StackPane 的区域。但是当我向它添加填充或边框等插图时,我遇到了麻烦。这是 class:
的简化示例public class CustomPane extends Region
{
private ToggleButton testControl = new ToggleButton("just a test control");
private StackPane rootPane = new StackPane(testControl);
public CustomPane()
{
getChildren().add(rootPane);
setStyle("-fx-border-color: #257165; -fx-border-width: 10;");
}
}
这就是结果的样子:
如果我尝试通过调用 那么该区域就会增长: 但我真的希望它看起来像这样: (第三张图片是通过扩展 StackPane 而不是 Region 创建的,后者已经正确管理了布局内容。不幸的是,我必须扩展 Region,因为我想保持 好的,我尝试处理布局计算,但我没有想到。专家可以给我一些建议吗?rootPane.setLayoutX(10);
rootPane.setLayoutY(10);
getChildren()
受保护。)
StackPane
将 insets
for layouting the (managed) children. Region
doesn't do this by default. Therefore you need to override the layoutChildren
与使用这些 insets
的东西一起使用,例如:
@Override
protected void layoutChildren() {
Insets insets = getInsets();
double top = insets.getTop(),
left = insets.getLeft(),
width = getWidth() - left - insets.getRight(),
height = getHeight() - top - insets.getBottom();
// layout all managed children (there's only rootPane in this case)
layoutInArea(rootPane,
left, top, // offset of layout area
width, height, // available size for content
0,
HPos.LEFT,
VPos.TOP);
}