JavaFX - 布局 BorderPane
-
简述
如果我们使用 BorderPane,节点会排列在 Top、Left、Right、Bottom 和 Center 位置。类名为 BorderPane 包裹的 javafx.scene.layout 代表 BorderPane。此类包含五个属性,其中包括 --
bottom - 属性是 Node类型,它代表放置在 BorderPane 底部的节点。您可以使用 setter 方法为此属性设置值setBottom().
-
center - 属性是 Node类型,它代表放置在 BorderPane 中心的节点。您可以使用 setter 方法为此属性设置值setCenter().
-
left - 属性是 Node类型,它表示放置在 BorderPane 左侧的节点。您可以使用 setter 方法为此属性设置值setLeft().
-
right - 属性是 Node类型,它代表放置在 BorderPane 右侧的节点。您可以使用 setter 方法为此属性设置值setRight().
-
top - 属性是 Node类型,它代表放置在 BorderPane 顶部的节点。您可以使用 setter 方法为此属性设置值setTop().
除了这些,这个类还提供以下方法 --
setMoognment()- 该方法用于设置属于该窗格的节点的对齐方式。此方法接受一个节点和一个优先级值。
-
-
例子
下面的程序是一个例子 BorderPane布局。在此,我们将在顶部、底部、右侧、左侧和中心位置插入五个文本字段。将此代码保存在名称为的文件中 BorderPaneExample.java.import javafx.application.Application; import javafx.collections.ObservableList; import javafx.scene.Scene; import javafx.scene.control.TextField; import javafx.scene.layout.BorderPane; import javafx.stage.Stage; public class BorderPaneExample extends Application { @Override public void start(Stage stage) { //Instantiating the BorderPane class BorderPane bPane = new BorderPane(); //Setting the top, bottom, center, right and left nodes to the pane bPane.setTop(new TextField("Top")); bPane.setBottom(new TextField("Bottom")); bPane.setLeft(new TextField("Left")); bPane.setRight(new TextField("Right")); bPane.setCenter(new TextField("Center")); //Creating a scene object Scene scene = new Scene(bPane); //Setting title to the Stage stage.setTitle("BorderPane Example"); //Adding scene to the stage stage.setScene(scene); //Displaying the contents of the stage stage.show(); } public static void main(String args[]){ launch(args); } }
使用以下命令从命令提示符编译并执行保存的 java 文件。javac BorderPaneExample.java java BorderPaneExample
执行时,上述程序会生成一个 JavaFX 窗口,如下所示。