Javafx 简明教程
JavaFX - SplitPane
splitpane 是一种控件,包含两个或更多由分隔线分开的滑动条。每个滑动条都提供一个不同的视口,使我们能够嵌入多个组件。我们还可以将拆分窗格的方向设置为水平或垂直。我们来看看典型的拆分窗格是什么样的:
The splitpane is a control with two or more slides separated by dividers. Each slide provides a different viewport allowing us to embed multiple components. We can also set the orientation of the splitpane to be horizontal or vertical. Let’s have a look at what a typical splitpane looks like −
SplitPane in JavaFX
在 JavaFX 中,名为 SplitPane 的类表示分割窗格。要使用分割窗格的功能,我们需要创建 SplitPane 类的实例并添加我们想要在其中显示的组件。这些组件可以是任何 JavaFX 节点,例如标签、按钮、图像、文本字段等。我们可以使用以下任何构造函数来创建一个分割窗格 −
In JavaFX, the class named SplitPane represents a splitpane. To use the feature of spiltpane, we need to create an instance of the SplitPane class and add the components we want to display inside it. These components can be any JavaFX nodes, such as labels, buttons, images, text fields and so on. We can use any of the below constructors to create a splitpane −
-
SplitPane() − It is the default constructor used to create a splitpane without any predefined nodes.
-
SplitPane(Node components) − It is the parameterized constructor of SplitPane class which will create a new splitpane with the specified nodes.
Steps to create a splitpane in JavaFX
要在 JavaFX 中创建一个分割窗格,请按照以下步骤操作。
To create a splitpane in JavaFX, follow the steps given below.
Step 1: Create two or more Nodes
正如前面所讨论的,一个分割窗格至少包含两张幻灯片。因此,我们的第一步是在这些不同的幻灯片中创建两个或更多节点以显示。为了这个示例,我们将使用标签。在 JavaFX 中,标签是通过实例化名为 Label 的类来创建的,该类属于 javafx.scene.control 包。如下所示创建标签 −
As discussed earlier, a splitpane contains at least two slides. Hence, our first step would be creating two or more nodes to display within these distinct slides. For the sake of this example, we are going to use the labels. In JavaFX, the labels are created by instantiating the class named Label which belongs to a package javafx.scene.control. Create the labels as shown below −
// Creating four labels
Label labelOne = new Label("Label 1");
Label labelTwo = new Label("Label 2");
Label labelThree = new Label("Label 3");
Label labelFour = new Label("Label 4");
同样,通过实例化各个类来创建所需的节点。
Similarly, create the desired nodes by instantiating their respective classes.
Step 2: Instantiate the SplitPane class
实例化 SplitPane 包的 javafx.scene.control 类,而不向其构造函数传递任何参数值,并使用 getItems() 方法将所有标签添加到分割窗格。
Instantiate the SplitPane class of package javafx.scene.control without passing any parameter value to its constructor and add all the labels to the splitpane using the getItems() method.
// instantiating the SplitPane class
SplitPane splitP = new SplitPane();
// adding the labels to the SplitPane
splitP.getItems().addAll(labelOne, labelTwo, labelThree, labelFour);
Step 3: Launching Application
在创建 SplitPane 并向其添加标签之后,按照以下步骤正确启动应用程序 −
After creating the SplitPane and adding the labels to it, follow the given steps below to launch the application properly −
-
Firstly, instantiate the class named Scene by passing the SplitPane object as a parameter value to its constructor. Also, pass dimensions of the application screen as optional parameters to this constructor.
-
Then, set the title to the stage using the setTitle() method of the Stage class.
-
Now, a Scene object is added to the stage using the setScene() method of the class named Stage.
-
Display the contents of the scene using the method named show().
-
Lastly, the application is launched with the help of the launch() method.
Example
下面是将在 JavaFX 中创建一个分割窗格的程序。将此代码保存在名为 NewSplitpane.java 的文件中。
Following is the program which will create a splitpane in JavaFX. Save this code in a file with the name NewSplitpane.java.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.SplitPane;
import javafx.stage.Stage;
public class NewSplitpane extends Application {
@Override
public void start(Stage stage) {
// Creating four labels
Label labelOne = new Label("Label 1");
Label labelTwo = new Label("Label 2");
Label labelThree = new Label("Label 3");
Label labelFour = new Label("Label 4");
// instantiating the SplitPane class
SplitPane splitP = new SplitPane();
// adding the labels to the SplitPane
splitP.getItems().addAll(labelOne, labelTwo, labelThree, labelFour);
// Creating a Scene with the SplitPane as its root node
Scene scene = new Scene(splitP, 400, 300);
// to set the title
stage.setTitle("SplitPane in JavaFX");
// Setting the Scene of the Stage
stage.setScene(scene);
// Display the Stage
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
使用以下命令通过命令提示符编译并执行上述 Java 文件。
Compile and execute the above Java file using the command prompt with the help of following commands.
javac --module-path %PATH_TO_FX% --add-modules javafx.controls NewSplitpane.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls NewSplitpane
在上一个程序执行时,它将生成一个 JavaFX 窗口,其中显示一个 SplitPane,其中有四个标签和三个分隔符,如下所示。
On executing, the above program generates a JavaFX window displaying a SplitPane with four labels and three dividers as shown below.
Setting the Orientation of the SplitPane
默认情况下, SplitPane 是水平方向的,这意味着这些组件从左到右彼此相邻放置。我们可以通过调用 SplitPane 类的 setOrientation() 方法并传递 Orientation.VERTICAL 参数来更改方向为垂直。
By default, the SplitPane has a horizontal orientation, meaning that the components are placed next to each other from left to right. We can change the orientation to vertical by calling the setOrientation() method of the SplitPane class and passing an Orientation.VERTICAL argument.
Example
在下面的 JavaFX 程序中,我们将创建一个垂直 SplitPane。将此代码保存在名为 SplitpaneDemo.java 的文件中。
In the following JavaFX program, we will create a vertical SplitPane. Save this code in a file with the name SplitpaneDemo.java.
import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.SplitPane;
import javafx.stage.Stage;
import javafx.scene.layout.VBox;
public class SplitpaneDemo extends Application {
@Override
public void start(Stage stage) {
// Create a SplitPane with vertical orientation
SplitPane splitP = new SplitPane();
splitP.setOrientation(Orientation.VERTICAL);
// vertical box to hold the labels
VBox box1 = new VBox();
VBox box2 = new VBox();
// Create two labels and add them to the SplitPane
box1.getChildren().add(new Label("This is \nthe \nfirst section"));
box2.getChildren().add(new Label("This is \nthe \nsecond section"));
splitP.getItems().addAll(box1, box2);
// Set the divider position to 50
splitP.setDividerPositions(0.5);
// Create a scene and show the stage
Scene scene = new Scene(splitP, 400, 300);
stage.setTitle("SplitPane in JavaFX");
stage.setScene(scene);
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 SplitpaneDemo.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls SplitpaneDemo
当我们执行上述代码时,它将生成以下输出。
When we execute the above code, it will generate the following output.