Javafx 简明教程
JavaFX - Layout Panes(Containers)
在场景中构建所有必需的节点后,我们通常按所需顺序排列它们。我们排列组件其中的容器称为容器的 Layout 。我们也可以说我们遵循了一个布局,因为它有助于将所有组件放置在容器内的特定位置。以下是说明在垂直和水平布局中放置 JavaFX 节点的图表。
After constructing all the required nodes in a scene, we generally arrange them in the desired order. The container in which we arrange the components is called the Layout of the container. We can also say that we followed a layout as it helps in placing all the components at a particular position within the container. Below is a diagram illustrating the positioning of JavaFX nodes in vertical and horizontal layout.
在本教程中,我们将讨论 JavaFX 提供的各种预定义布局,包括 HBox, VBox, Border Pane, Stack Pane, Text Flow, Anchor Pane, Title Pane, Grid Pane, Flow Panel 等。上面提到的每个布局都由一个类表示,所有这些类都属于名为 javafx.layout 的包。名为 Pane 的类是 JavaFX 中所有布局的基类。
In this tutorial, we are going to discuss various predefined layouts provided by JavaFX including HBox, VBox, Border Pane, Stack Pane, Text Flow, Anchor Pane, Title Pane, Grid Pane, Flow Panel, and so on. Each of the above mentioned layout is represented by a class and all these classes belongs to the package named javafx.layout. The class named Pane is the base class of all the layouts in JavaFX.
Creating a Layout
要创建布局,我们需要遵循以下步骤:
To create a layout, we need to follow the given steps −
-
Create node.
-
Instantiate the respective class of the required layout.
-
Set the properties of the layout.
-
Add all the created nodes to the layout.
Creating Nodes
首先,通过实例化其各自的类来创建 JavaFX 应用程序所需节点。例如,如果我们希望在 HBox 布局中有一个文本字段和两个按钮(即播放和停止),我们必须首先创建这些节点,如下所示代码块:
First of all, create the required nodes of the JavaFX application by instantiating their respective classes. For example, if we want to have a text field and two buttons namely, play and stop in a HBox layout, we will have to initially create those nodes as shown in the following code block −
//Creating a text field
TextField textField = new TextField();
//Creating the play button
Button playButton = new Button("Play");
//Creating the stop button
Button stopButton = new Button("stop");
Instantiating the Respective Class
在创建节点(并在其上完成所有操作)后,实例化所需布局的类。例如,如果我们希望创建 Hbox 布局,我们需要按如下方式实例化该类:
After creating the nodes (and completing all the operations on them), instantiate the class of the required layout. For example, if we want to create a Hbox layout, we need to instantiate this class as follows −
HBox hbox = new HBox();
Setting the Properties of the Layout
在实例化该类后,我们需要使用其各自的 setter 方法设置布局的属性。例如:如果我们希望在 HBox 布局中设置创建的节点之间的间距,那么我们需要将值设置为名为 spacing 的属性。这可以通过使用 setter 方法 setSpacing() 来完成,如下所示:
After instantiating the class, we need to set the properties of the layout using their respective setter methods. For instance − If we want to set space between the created nodes in the HBox layout, then we need to set value to the property named spacing. This can be done by using the setter method setSpacing() as shown below −
hbox.setSpacing(10);
Adding the node Objects to the Layout Pane
最后,我们需要将创建的节点的对象添加到布局窗格中,方法是将其作为参数值传递,如下所示:
Finally, we need to add the objects of the created nodes to the layout pane by passing them as a parameter value as shown below −
//Creating a Group object
hbox.getChildren().addAll(textField, playButton, stopButton);
Layout Panes in JavaFX
以下是 JavaFX 提供的各种布局窗格(类)。这些类存在于 javafx.scene.layout 包中。
Following are the various Layout panes (classes) provided by JavaFX. These classes exist in the package javafx.scene.layout.
S.No |
Layouts & Description |
1 |
HBoxThe HBox layout arranges all the nodes in our application in a single horizontal row. The class named HBox of the package javafx.scene.layout represents the text horizontal box layout. |
2 |
VBoxThe VBox layout arranges all the nodes in our application in a single vertical column. The class named VBox of the package javafx.scene.layout represents the text Vertical box layout. |
3 |
BorderPaneThe Border Pane layout arranges the nodes in our application in top, left, right, bottom and center positions. The class named BorderPane of the package javafx.scene.layout represents the border pane layout. |
4 |
StackPaneThe stack pane layout arranges the nodes in our application on top of another just like in a stack. The node added first is placed at the bottom of the stack and the next node is placed on top of it. The class named StackPane of the package javafx.scene.layout represents the stack pane layout. |
5 |
TextFlowThe Text Flow layout arranges multiple text nodes in a single flow. The class named TextFlow of the package javafx.scene.layout represents the text flow layout. |
6 |
AnchorPaneThe Anchor pane layout anchors the nodes in our application at a particular distance from the pane. The class named AnchorPane of the package javafx.scene.layout represents the Anchor Pane layout. |
7 |
TilePaneThe Tile Pane layout adds all the nodes of our application in the form of uniformly sized tiles. The class named TilePane of the package javafx.scene.layout represents the TilePane layout. |
8 |
GridPaneThe Grid Pane layout arranges the nodes in our application as a grid of rows and columns. This layout comes handy while creating forms using JavaFX. The class named GridPane of the package javafx.scene.layout represents the GridPane layout. |
9 |
FlowPaneThe flow pane layout wraps all the nodes in a flow. A horizontal flow pane wraps the elements of the pane at its height, while a vertical flow pane wraps the elements at its width. The class named FlowPane of the package javafx.scene.layout represents the Flow Pane layout. |