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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20735797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:56:36  来源:igfitidea点击:

JavaFX - How can i add a container in an anchor pane

javajavafxfxml

提问by ciprianr

I have a simple project that has a fxml with a splitter.

我有一个简单的项目,它有一个带拆分器的 fxml。

So the fxml is this:

所以 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>

What i would want is to insert a vbox in the left anchor pane from the splitter using only java code.

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

Can this be done?

这能做到吗?

I am new to fxml so any help would be apreciated.

我是 fxml 的新手,所以任何帮助都会受到赞赏。

Thank you in advance.

先感谢您。

采纳答案by Nikos Paraskevopoulos

Add an fx:idto the AnchorPaneyou want to manipulate:

添加一个fx:idAnchorPane你想要操作的:

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

Get it in your controller as a @FXMLmember field:

在您的控制器中将其作为@FXML成员字段获取:

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

And manipulate it in the desired place (initialize()shown here, can be -almost- anywhere else):

并在所需的位置操作它(initialize()如图所示,几乎可以在其他任何地方):

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

回答by Givver

You should try the JavaFX Scene Builder. It makes editing your fxml much easier as it is all done graphically.

您应该尝试使用 JavaFX Scene Builder。它使编辑 fxml 变得更加容易,因为它都是以图形方式完成的。

http://www.oracle.com/technetwork/java/javafx/tools/index.html

http://www.oracle.com/technetwork/java/javafx/tools/index.html