java - 如何使 borderpane 中的右侧节点占据整个右侧

标签 java javafx fxml borderpane

我在 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/

相关文章:

Java 将 <object> 列表转换到它的接口(interface)

javafx - 关注容器中的下一个项目

java - 带滚动条的只读文本区域 [JavaFx]

JavaFX 图像每次添加到布局时都会落在 Y 轴上

java - 设置自定义 TextArea 背景的秘诀是什么?

javafx-2 - 在javafx 2.0中设计GUI的最佳方法是什么?

java - Android 应用程序在建立 HTTP url 连接时出现滞后

java - 在selenium中选择cssselection时显示异常

使用 translateX() 和 translateY() 时, Pane 布局中的 JavaFX 文本与其他元素重叠

java - 使用线程在 Swing (Java) 中实现 MVC 范例