Javafx 简明教程

JavaFX - BorderPane Layout

BorderPane Layout in JavaFX

BorderPane 是一种布局控件,它可以将 JavaFX 应用程序的所有 UI 组件布置到五个区别的区域中,即顶部、左侧、右侧、底部和中心位置。BorderPane 布局窗格由 BorderPane 包的 javafx.scene.layout 类的类表示。实例化此类会创建一个 BorderPane 布局。此类的构造函数如下所示:

The BorderPane is a layout control that arranges all the UI components of a JavaFX application into five distinc regions namely Top, Left, Right, Bottom and Center positions. The BorderPane layout pane is represented by a class named BorderPane of the javafx.scene.layout package. Instantiating this class will create a BorderPane layout. Constructors of this class are as follows −

  1. BorderPane() − It is the default constructor that creates an empty BorderPane.

  2. BorderPane(Node centerNode) − It constructs a new BorderPane layout and positions the node in the center.

  3. BorderPane(Node center, Node top, Node right, Node bottom, Node left) − This parameterized constructor of BorderPane class used to create a new BorderPane layout with the specified nodes.

BorderPane 类包含五个属性,包括:

The BorderPane class contains five properties, which include −

  1. bottom − This property is of Node type and it represents the node placed at the bottom of the BorderPane. You can set value to this property using the setter method setBottom().

  2. center − This property is of Node type and it represents the node placed at the center of the BorderPane. You can set value to this property using the setter method setCenter().

  3. left − This property is of Node type and it represents the node placed at the left of the BorderPane. You can set value to this property using the setter method setLeft().

  4. right − This property is of Node type and it represents the node placed at the right of the BorderPane. You can set value to this property using the setter method setRight().

  5. top − This property is of Node type and it represents the node placed at the top of the BorderPane. You can set value to this property using the setter method setTop().

下图显示了如何在 BorderPane 布局中布置 JavaFX 节点:

The figure below shows how JavaFX nodes are arranged in a BorderPane layout −

sample borderpane

除了上面提到的属性和构造函数之外, BorderPane 类还提供以下方法:

In addition to the above mentioned properties and constructors, the BorderPane class also provides the following method −

  1. setAlignment() − This method is used to set the alignment of the nodes belonging to this pane. This method accepts a node and a priority value.

Example

以下程序是 BorderPane 布局的一个示例。在此示例中,我们正在顶部、底部、右侧、左侧和中心位置插入五个文本字段。将此代码保存在名为 BorderPaneExample.java 的文件中:

The following program is an example of the BorderPane layout. In this, we are inserting a five text fields in the Top, Bottom, Right, Left and Center positions. Save this code in a file with the name 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, 400, 300);

      //Setting title to the Stage
      stage.setTitle("BorderPane in JavaFX");

      //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 文件。

Compile and execute the saved java file from the command prompt using the following commands.

javac --module-path %PATH_TO_FX% --add-modules javafx.controls BorderPaneExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls BorderPaneExample

执行后,上述程序会生成如下所示的 JavaFX 窗口。

On executing, the above program generates a JavaFX window as shown below.

borderpane output