我在 BorderPane 的右侧有一个 AnchorPane,在底部有另一个 AnchorPane。我想知道最好的方法,考虑到可调整大小,使右侧 AncorPane 占据整个右侧,并使底部 AnchorPane 结束于右侧 AnchorPane 的开始位置。
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1">
<bottom>
<AnchorPane minWidth="200.0" prefHeight="45.0" prefWidth="600.0" BorderPane.alignment="CENTER" />
</bottom>
<right>
<AnchorPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</right>
</BorderPane>
最佳答案
首先,我建议使用常规 Pane
而不是 AnchorPane
。
然后,您可以将其放入代码中的 start
方法内。
stage.widthProperty().addListener(event -> {
rightPane.setPrefWidth(stage.getWidth()/2);
rightPane.relocate(stage.getWidth()/2,0);
});
stage.heightProperty().addListener(event -> {
rightPane.setPrefHeight(stage.getHeight());
});
root.getChildren().add(pane);
这会生成一个占据右侧一半舞台或窗口的 Pane 。
然后,在您的 fxml 文件中,在
<bottom>
<AnchorPane minWidth="200.0" prefHeight="45.0" prefWidth="600.0" BorderPane.alignment="CENTER" />
</bottom>
<right>
<AnchorPane prefHeight="200.0" prefWidth="200.0" BorderPane.alignment="CENTER" />
</right>
删除所有说明 Pane 位置和宽度的内容。
然后,对于底部 Pane ,您可以添加到 widthProperty()
和 heightProperty()
监听器来获取以下内容:
stage.widthProperty().addListener(event -> {
rightPane.setPrefWidth(stage.getWidth()/2);
rightPane.relocate(stage.getWidth()/2,0);
bottomPane.setPrefWidth(stage.getWidth()-rightPane.getPrefWidth());
});
stage.heightProperty().addListener(event -> {
rightPane.setPrefHeight(stage.getHeight());
bottomPane.setPrefHeight(100); // change 100 to how high you want your bottomPane to be
//if you want your bottomPane's height to be half the height of the window use this: bottomPane.setPrefHeight(stage.getHeight()/2);
bottomPane.relocate(0,stage.getHeight() - bottomPane.getPrefHeight());
bottomPane.toFront();
});
关于java - 如何使 borderpane 中的右侧节点占据整个右侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55326959/