如何在fxml中设置BorderPane的边距?

时间:2016-06-07 22:05:02

标签: javafx fxml

我正在尝试在javafx中运行此fxml代码:

<BorderPane fx:controller="com.bryantmorrill.chat.main.Controller"
      xmlns:fx="http://javafx.com/fxml" >

<center>
    <ScrollPane BorderPane.margin="25, 25, 25, 25">
        <content>
            <TextArea fx:id="chatArea" minWidth="200" maxWidth="450"
                      prefWidth="450" minHeight="200" prefHeight="400"
                      maxHeight="400"/>
        </content>
    </ScrollPane>
</center>

<bottom>
    <FlowPane BorderPane.margin="25, 25, 25, 25">
        <TextField fx:id="inputArea" minWidth="200" maxWidth="450" prefWidth="450"/>
        <Button text="Send" onAction="#sendMessage" minWidth="200" maxWidth="450" prefWidth="450"/>
    </FlowPane>

</bottom>

但是,当我尝试以这种方式设置边距时失败:

<ScrollPane BorderPane.margin="25, 25, 25, 25">

我也试过这些方法:

<ScrollPane BorderPane.margin="25 25 25 25">
<ScrollPane BorderPane.margin="25">

这是我得到的所有例外:

java.lang.IllegalArgumentException: Unable to coerce 25, 25, 25, 25 to class javafx.geometry.Insets.

这是我第一次使用JavaFX,但我找不到任何好的例子。谢谢你的帮助!

1 个答案:

答案 0 :(得分:7)

您需要将边距添加为BorderPane的子节点的子元素:

<center>
    <ScrollPane>
        <BorderPane.margin>
             <Insets bottom="25.0" left="25.0" right="25.0" top="25.0" />
        </BorderPane.margin>
        <content>
            <TextArea fx:id="chatArea" minWidth="200" maxWidth="450"
                      prefWidth="450" minHeight="200" prefHeight="400"
                      maxHeight="400"/>
        </content>
    </ScrollPane>
</center>
<bottom>
    <FlowPane>
        <BorderPane.margin>
             <Insets bottom="25.0" left="25.0" right="25.0" top="25.0" />
        </BorderPane.margin>
        <TextField fx:id="inputArea" minWidth="200" maxWidth="450" prefWidth="450"/>
        <Button text="Send" onAction="#sendMessage" minWidth="200" maxWidth="450" prefWidth="450"/>
    </FlowPane>
</bottom>