📜  JavaFX |文本流类

📅  最后修改于: 2022-05-13 01:55:25.631000             🧑  作者: Mango

JavaFX |文本流类

TextFlow 类是 JavaFX 的一部分。 TextFlow 类旨在布局富文本。它可用于在单个文本流中布局多个文本节点。 TextFlow 类扩展了 Pane类。

类的构造函数:

  1. TextFlow() :创建一个新的文本流对象。
  2. TextFlow(Node... c) : 创建一个具有指定节点的新文本流对象。

常用方法:

MethodExplanation
getLineSpacing()Returns the line spacing of the text flow
getTextAlignment()Returns the text alignment of the text flow
setLineSpacing(double s)Set line spacing of the text flow .
setTextAlignment(TextAlignment v)Sets the text alignment of the text flow.

下面的程序说明了 TextFlow 类的使用:

  1. 用于创建 TextFlow 并向其添加文本对象的Java程序:在此程序中,我们将创建一个名为text_flow的 TextFlow 和两个名为text_1text_2的 Text。使用setFill()setFont()设置填充和字体。我们将使用getChildren().add()函数将文本添加到text_flow 。将text_flow添加到场景,将场景添加到舞台。调用show()函数以显示最终结果。
    // Java program to create a TextFlow and 
    // add text object to it .
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.event.ActionEvent;
    import javafx.scene.paint.*;
    import javafx.scene.text.*;
    import javafx.scene.web.*;
    import javafx.scene.layout.*;
    import javafx.scene.shape.*;
      
    public class TextFlow_0 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
      
            try {
      
                // set title for the stage
                stage.setTitle("TextFlow");
      
                // create TextFlow
                TextFlow text_flow = new TextFlow();
      
                // create text
                Text text_1 = new Text("GeeksforGeeks\n");
      
                // set the text color
                text_1.setFill(Color.RED);
      
                // set font of the text
                text_1.setFont(Font.font("Verdana", 25));
      
                // create text
                Text text_2 = new Text("The computer science portal for geeks");
      
                // set the text color
                text_2.setFill(Color.BLUE);
      
                // set font of the text
                text_2.setFont(Font.font("Helvetica", FontPosture.ITALIC, 15));
      
                // add text to textflow
                text_flow.getChildren().add(text_1);
                text_flow.getChildren().add(text_2);
      
                // create a scene
                Scene scene = new Scene(text_flow, 400, 300);
      
                // set the scene
                stage.setScene(scene);
      
                stage.show();
            }
      
            catch (Exception e) {
      
                System.out.println(e.getMessage());
            }
        }
      
        // Main Method
        public static void main(String args[])
        {
      
            // launch the application
            launch(args);
        }
    }
    

    输出:

  2. Java程序创建一个 TextFlow 并向其添加文本对象,设置文本对齐方式并设置文本流的行距:在这个程序中,我们将创建一个名为text_flow的 TextFlow 和两个名为text_1text_2的文本。使用setFill()setFont()设置填充和字体。使用setTextAlignment()设置 TextAlignment 并使用setLineSpacing()函数设置行距。使用getChildren().add()函数将文本添加到text_flow 。将text_flow添加到 Vbox。将vbox场景和场景添加到舞台。调用show()函数以显示最终结果。
    // Java program to create a TextFlow and 
    // add text object to it, set text Alignment
    // and set line spacing of the text flow.
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.scene.layout.*;
    import javafx.scene.paint.*;
    import javafx.scene.text.*;
    import javafx.geometry.*;
    import javafx.scene.layout.*;
    import javafx.scene.shape.*;
      
    public class TextFlow_1 extends Application {
      
        // launch the application
        public void start(Stage stage)
        {
      
            try {
      
                // set title for the stage
                stage.setTitle("FlowPane");
      
                // create TextFlow
                TextFlow text_flow = new TextFlow();
      
                // create text
                Text text_1 = new Text("GeeksforGeeks\n");
      
                // set the text color
                text_1.setFill(Color.GREEN);
      
                // set font of the text
                text_1.setFont(Font.font("Verdana", 25));
      
                // create text
                Text text_2 = new Text("The computer science portal for geeks");
      
                // set the text color
                text_2.setFill(Color.BLUE);
      
                // set font of the text
                text_2.setFont(Font.font("Helvetica", FontPosture.ITALIC, 15));
      
                // add text to textflow
                text_flow.getChildren().add(text_1);
                text_flow.getChildren().add(text_2);
      
                // set text Alignment
                text_flow.setTextAlignment(TextAlignment.CENTER);
      
                // set line spacing
                text_flow.setLineSpacing(20.0f);
      
                // create VBox
                VBox vbox = new VBox(text_flow);
      
                // set alignment of vbox
                vbox.setAlignment(Pos.CENTER);
      
                // create a scene
                Scene scene = new Scene(vbox, 400, 300);
      
                // set the scene
                stage.setScene(scene);
      
                stage.show();
            }
      
            catch (Exception e) {
      
                System.out.println(e.getMessage());
            }
        }
      
        // Main Method
        public static void main(String args[])
        {
      
            // launch the application
            launch(args);
        }
    }
    

    输出:

注意:以上程序可能无法在在线 IDE 中运行,请使用离线编译器。

参考: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/text/TextFlow.html