加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

JavaFX – 如何在锚点窗格中添加容器

发布时间:2020-12-15 05:02:31 所属栏目:Java 来源:网络整理
导读:我有一个简单的项目,其中包含带分割器的fxml. 所以fxml是这样的: AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="accordionproject.FXMLDocumentCo
我有一个简单的项目,其中包含带分割器的fxml.

所以fxml是这样的:

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="accordionproject.FXMLDocumentController">
    <children>
        <SplitPane fx:id="splitPane" dividerPositions="0.29797979797979796" focusTraversable="true" layoutX="60.0" layoutY="14.0" prefHeight="200.0" prefWidth="320.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" xmlns:fx="http://javafx.com/fxml">
            <items>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" />
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0" />
            </items>
        </SplitPane>
    </children>
</AnchorPane>

我想要的是仅使用java代码在分割器的左锚定窗格中插入一个vbox.

可以这样做吗?

我是fxml的新手,所以任何帮助都会被贬低.

先感谢您.

解决方法

将fx:id添加到要操作的AnchorPane:

<AnchorPane fx:id="leftAnchorPane" minHeight="0.0" minWidth="0.0"
    prefHeight="160.0" prefWidth="100.0" />

作为@FXML成员字段在控制器中获取它:

public class FXMLDocumentController
{
    @FXML private AnchorPane leftAnchorPane;
    ...
}

并在所需的位置操作它(此处显示的initialize(),可以是 – 几乎 – 其他任何地方):

public void initialize() {
    VBox vbox = new VBox();
    ...
    AnchorPane.setTopAnchor(vbox,10.0); // obviously provide your own constraints
    leftAnchorPane.getChildren().add(vbox);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读