Javafx 简明教程

JavaFX - Separator

separator 是水平或垂直线,用来划分 JavaFX 应用程序的 UI 元素。请注意,它不会产生任何动作。但是,可以对其进行样式化、应用视觉效果,甚至对其进行动画处理以提供更好的用户体验。在下面的图片中,我们可以看到一个垂直分隔符和一个水平分隔符 −

A separator is a horizontal or vertical line that serves to divide UI elements of the JavaFX application. Note that it does not produce any action. However, it is possible to style it, apply visual effects to it, or even animate it to provide a better user experience. In the below figure, we can see a vertical and a horizontal separator −

separator

Separator in JavaFX

在 JavaFX 中,分隔符由名为 Separator 的类表示。此类属于 javafx.scene.control 包。通过实例化此类,可以在 JavaFX 中创建一个分隔符。此类具有以下构造函数 −

In JavaFX, the separator is represented by a class named Separator. This class belongs to the package javafx.scene.control. By instantiating this class, we can create a separator in JavaFX. This class has the following constructors −

  1. Separator() − It creates a new horizontal separator.

  2. Separator(Orientation orientation) − It creates a separator with the specified orientation. The orientation could be either vertical or horizontal.

Creating a Separator in JavaFX

我们需要按照以下步骤在 JavaFX 中创建分隔符。

We need to follow the steps given below to create a Separator in JavaFX.

Step 1: Create nodes that are going to be separated

因为分隔符用于分隔 JavaFX 中的 UI 节点。因此,需要指定要分隔的 UI 元素。例如,我们使用下面的代码创建按钮 −

Since separators are used to separate UI nodes in JavaFX. Hence, it is required to specify the UI elements that are going to be separated. For instance, we are creating Buttons using the below code −

// Creating Buttons
Button btn1 = new Button("Button 1");
Button btn2 = new Button("Button 2");
Button btn3 = new Button("Button 3");
Button btn4 = new Button("Button 4");

Step 2: Instantiate the Separator class

在 JavaFX 中,通过实例化名为 Separators 的类来创建分隔符,该类属于 * javafx.scene.control* 包。在 start() 方法中实例化该类,如下所示 −

In JavaFX, the separators are created by instantiating the class named Separators which belongs to a package * javafx.scene.control*. Instantiate this class inside the start() method as shown below −

// creating a separator
Separator sep1 = new Separator();

Step 3: Set the desired Orientation

默认情况下,分隔符是水平的,但是,我们可以使用 setOrientation() 方法并传递方向枚举值 (HORIZONTAL 或 VERTICAL) 作为参数来更改其方向。下面显示的代码将方向设置为垂直 −

By default, the separator is horizontal, but, we can change its orientation by using the setOrientation() method and passing an Orientation enum value (HORIZONTAL or VERTICAL) as an argument. The code shown below will set the orientation to vertical −

sep1.setOrientation(Orientation.VERTICAL);

此外,分隔符占用分配给它的全部水平或垂直空间。因此,有必要使用 setMaxWidth()setMaxHeight 方法根据要求设置其最大宽度或高度。若要指定分配的空间中分隔符线的水平或垂直对齐方式,请使用 setHalignment 或 setValignment 方法并传递 HPos 或 VPos 枚举值作为参数。

Also, the Separator occupies the full horizontal or vertical space allocated to it. Hence, it is necessary to set its maximum width or height as per the requirement by using the setMaxWidth() or setMaxHeight methods. To specify the horizontal or vertical alignment of the separator line within the allocated space, use the setHalignment or setValignment methods and pass an HPos or VPos enum value as an argument.

Step 4: Launch the Application

创建分隔符后,按照下面给出的步骤正确启动应用程序 −

Once the Separator is created, follow the given steps below to launch the application properly −

  1. Firstly, instantiate the class named Scene by passing the ChoiceBox object as a parameter value to its constructor. We can also pass dimensions of the application screen as optional parameters to this constructor.

  2. Then, set the title to the stage using the setTitle() method of the Stage class.

  3. Now, a Scene object is added to the stage using the setScene() method of the class named Stage.

  4. Display the contents of the scene using the method named show().

  5. Lastly, the application is launched with the help of the launch() method.

Example

以下 JavaFX 程序演示如何在 JavaFX 应用程序中创建按钮之间的分隔符。将该代码保存在名为 JavafxSeparator.java 的文件中。

The following JavaFX program demonstrates how to create a separator between buttons in JavaFX application. Save this code in a file with the name JavafxSeparator.java.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Separator;
import javafx.scene.layout.GridPane;
import javafx.geometry.Orientation;
import javafx.stage.Stage;
import javafx.geometry.Pos;
public class JavafxSeparator extends Application {
   @Override
   public void start(Stage stage) {
      // creating grid to contain buttons
      GridPane newgrid = new GridPane();
      // set horizontal and vertical gap
      newgrid.setHgap(10);
      newgrid.setVgap(10);
      newgrid.setAlignment(Pos.CENTER);
      // Creating Buttons
      Button btn1 = new Button("Button 1");
      Button btn2 = new Button("Button 2");
      Button btn3 = new Button("Button 3");
      Button btn4 = new Button("Button 4");
      // adding buttons to the grid
      newgrid.add(btn1, 0, 0);
      newgrid.add(btn2, 2, 0);
      newgrid.add(btn3, 0, 1);
      newgrid.add(btn4, 2, 1);
      // creating a horizontal and a vertical separator
      Separator sep1 = new Separator();
      Separator sep2 = new Separator();
      sep2.setOrientation(Orientation.VERTICAL);
      // Setting background color to the separators
      sep1.setStyle("-fx-background-color: #D2691E;");
      sep2.setStyle("-fx-background-color: #D2691E;");
      // adding separators to the grid
      newgrid.add(sep1, 0, 2, 2, 1);
      newgrid.add(sep2, 1, 0, 1, 2);
      // creating scene and stage
      Scene scene = new Scene(newgrid, 400, 300);
      stage.setTitle("Separator in JavaFX");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String[] args) {
      launch(args);
   }
}

要从命令提示符编译并执行已保存的 Java 文件,请使用以下命令−

To compile and execute the saved Java file from the command prompt, use the following commands −

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

当我们执行上述代码时,它将生成以下输出。

When we execute the above code, it will generate the following output.

separator output

Creating a Separator in JavaFX using its parameterized constructor

如前所述,有两种方法可以在 JavaFX 中创建分隔符:一种使用分隔符类的默认构造函数,另一种使用其参数化构造函数。在下一个示例中,我们将使用 Separator 类的参数化构造函数在 JavaFX 中创建分隔符。将此代码保存在名为 VerticalSeparator.java 的文件中。

As previously discussed, there are two ways to create a separator in JavaFX: one utilizes the default constructor of the Separator class, while the other uses its parameterized constructor. In the next example, we are going to use the parameterized constructor of the Separator class to create a separator in JavaFX. Save this code in a file with the name VerticalSeparator.java.

Example

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Separator;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.geometry.Orientation;
public class VerticalSeparator extends Application {
   @Override
   public void start(Stage stage) {
      // creating three text fields
      TextField textField1 = new TextField();
      TextField textField2 = new TextField();
      TextField textField3 = new TextField();
      // creating vertical separators
      Separator separator1 = new Separator(Orientation.VERTICAL);
      Separator separator2 = new Separator(Orientation.VERTICAL);
      // creating HBox
      HBox hbox = new HBox();
      hbox.setPadding(new Insets(15, 12, 15, 12));
      hbox.setSpacing(10);
      hbox.getChildren().addAll(textField1, separator1, textField2, separator2, textField3);
      // setting the scene and stage
      Scene scene = new Scene(hbox, 500, 300);
      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 by using the following commands −

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

在执行上述代码时,它将生成以下输出。

On executing the above code, it will generate the following output.

separator output2