Javafx 简明教程

JavaFX - Textflow Layout

TextFlow Layout in JavaFX

TextFlow 是一个布局,它允许我们在单一流程中设置多个文本节点,并根据 TextFlow 的字体、宽度和行距调整它们的位置和对齐方式。它还可以嵌入可以插入到文本内容中的对象,例如图像或形状。包 javafx.scene.layout 的命名为 textFlow 的类表示文本流。要创建 TextFlow,我们可以使用以下列出的其一个构造函数 −

TextFlow is a layout that allows us to set multiple text nodes in a single flow, and adjust their position and alignment according to the font, width, and line spacing of the TextFlow. It can also embed objects, such as images or shapes, that can be inserted in the text content. The class named textFlow of the package javafx.scene.layout represents the text flow. To create a TextFlow, we can use one of its constructors listed below −

  1. TextFlow() − Creates an empty TextFlow.

  2. TextFlow(Node childNodes) − Creates a TextFlow with the given nodes as children.

为了自定义 TextFlow 的外观和行为,此类提供了两个属性,如下所示 −

To customize the appearance and behavior of the TextFlow, this class provides two properties which are as follows −

  1. lineSpacing − This property is of double type and it is used to define the space between the text objects. You can set this property using the method named setLineSpacing().

  2. textAlignment − This property represents the alignment of the text objects in the pane. You can set value to this property using the method setTextAlignment(). To this method you can pass four values: CENTER, JUSTIFY, LEFT, RIGHT.

Example

以下程序是文本流布局的一个示例。在此,我们使用字体 15 创建三个文本对象。使用名称 TextFlowExample.java 将这段代码保存到文件中。

The following program is an example of the text flow layout. In this, we are creating three text objects with font 15. Save this code in a file with the name TextFlowExample.java.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;

public class TextFlowExample extends Application {
   @Override
   public void start(Stage stage) {
      //Creating text objects
      Text text1 = new Text("Welcome to Tutorialspoint ");

      //Setting font to the text
      text1.setFont(new Font(15));

      //Setting color to the text
      text1.setFill(Color.DARKSLATEBLUE);

      Text text2 = new Text("We provide free tutorials for readers in various technologies  ");

      //Setting font to the text
      text2.setFont(new Font(15));

      //Setting color to the text
      text2.setFill(Color.DARKGOLDENROD);
      Text text3 = new Text("\n Recently we started free video tutorials too ");

      //Setting font to the text
      text3.setFont(new Font(15));

      //Setting color to the text
      text3.setFill(Color.DARKGRAY);

      Text text4 = new Text("We believe in easy learning");

      //Setting font to the text
      text4.setFont(new Font(15));
      text4.setFill(Color.MEDIUMVIOLETRED);

      //Creating the text flow plane
      TextFlow textFlowPane = new TextFlow();

      //Setting the line spacing between the text objects
      textFlowPane.setTextAlignment(TextAlignment.JUSTIFY);

      //Setting the width
      textFlowPane.setPrefSize(600, 300);

      //Setting the line spacing
      textFlowPane.setLineSpacing(5.0);

      //Adding cylinder to the pane
      textFlowPane.getChildren().addAll(text1, text2, text3, text4);

      //Creating a scene object
      Scene scene = new Scene(textFlowPane, 400, 300);

      //Setting title to the Stage
      stage.setTitle("Textflow Layout 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 TextFlowExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls TextflowExample

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

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

textflow output

Adding Images to the TextFlow

还可以嵌入在 TextFlow 中的图像。这是通过使用 JavaFX 的 ImageImageView 类来实现的。我们只需要实例化这些类并将 ImageView 对象传递给 TextFlow 类的构造函数,如下面示例所示。使用名称 JavafxTextflow.java 将这段代码保存到 Java 文件中。

It is also possible to embed image within the TextFlow. This is achieved by using the Image and ImageView class of the JavaFX. We simply need to instantiate these classes and pass the ImageView object to the constructor of TextFlow class as shown in the below example. Save this code in a Java file with the name JavafxTextflow.java.

Example

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;

public class JavafxTextflow extends Application {
   @Override
   public void start(Stage stage) {
      // Create some text nodes with different styles
      Text text1 = new Text("Hello");
      text1.setFill(Color.RED);
      text1.setFont(Font.font("Arial", FontWeight.BOLD, 20));

      Text text2 = new Text("JavaFX");
      text2.setFill(Color.BLUE);
      text2.setFont(Font.font("Arial", FontPosture.ITALIC, 20));

      Text text3 = new Text("\nThis is a ");
      text3.setFill(Color.BLACK);
      text3.setFont(Font.font("Arial", 16));

      Text text4 = new Text("TextFlow");
      text4.setFill(Color.GREEN);
      text4.setFont(Font.font("Arial", FontWeight.BOLD, FontPosture.ITALIC, 16));

      Text text5 = new Text(" example.");
      text5.setFill(Color.BLACK);
      text5.setFont(Font.font("Arial", 16));

      // Create an image node
      Image image = new Image("faviconTP.png");
      ImageView imageView = new ImageView(image);
      imageView.setFitWidth(100);
      imageView.setPreserveRatio(true);

      // Create a text flow with the text nodes and the image node
      TextFlow textFlow = new TextFlow(text1, text2, text3, text4, text5, imageView);

      // Set some properties of the text flow
      textFlow.setLineSpacing(10);
      textFlow.setTextAlignment(TextAlignment.CENTER);
      textFlow.setPrefWidth(300);

      // Create a scene and a stage
      Scene scene = new Scene(textFlow, 400, 300);
      stage.setTitle("TextFlow Example");
      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 JavafxTextflow.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxTextflow

执行上面的 Java 代码后,它将生成一个 JavaFX 窗口,如下所示。

On executing the above Java code, it will generate a JavaFX window as shown below.

textflow output2