HOWTO · Java

JavaFX 显示文本

本教程演示了如何在 JavaFX 中显示文本。

本页内容

可以使用 JavaFX.scene.text.Text 类创建和显示文本。本教程演示如何在 JavaFX 中显示单行和多行文本。

JavaFX 显示文本

JavaFX.scene,text.Text 用于在 JavaFX 中创建和显示文本。可以通过实例化 Text 类来创建文本节点并显示在场景中。

语法:

Text text = new Text(text);

其中 text 作为参数是文本值。要设置文本的 x 和 y 位置的值,我们使用以下方法:

text.setX(30);
text.setY(30);

上述方法将根据方法中给出的 x 和 y 位置设置文本的位置。按照以下步骤在 JavaFX 中创建和显示文本:

  1. 通过扩展 Application 类并实现 start() 方法来创建一个类。
  2. 通过实例化类 Text 创建文本。然后使用 setX() 和 setY() 方法设置 x 和 y 位置。
  3. 创建一个组类。
  4. 创建一个场景对象,实例化 scene 类,并将 group 对象传递给 scene。
  5. 通过 setTitle 方法为舞台添加标题,并使用 setScene() 方法将场景添加到舞台。
  6. 使用 show() 方法显示舞台并启动应用程序。

让我们根据上述步骤实现一个示例。

示例代码:

package delftstack;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class JavaFX_Display_Text extends Application {
  @Override
  public void start(Stage Demo_Stage) {
    // Create a Text object
    Text Demo_Text = new Text();

    // Set the text to be added.
    Demo_Text.setText("Hello, This is delftstack.com");

    // set the position of the text
    Demo_Text.setX(80);
    Demo_Text.setY(80);

    // Create a Group object
    Group Group_Root = new Group(Demo_Text);

    // Create a scene object
    Scene Demo_Scene = new Scene(Group_Root, 600, 300);

    // Set title to the Stage
    Demo_Stage.setTitle("Text Display");

    // Add scene to the stage
    Demo_Stage.setScene(Demo_Scene);

    // Display the contents of the stage
    Demo_Stage.show();
  }
  public static void main(String args[]) {
    launch(args);
  }
}

上面的代码将在场景中创建并显示 Text。

输出:

JavaFX 显示文本

我们可以使用 Label 而不是 Text 来显示多行文本。创建一个标签并将文本传递给它。

我们必须将 Text 包装在 Label 中以将其显示为多行文本。

示例代码:

package delftstack;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class JavaFX_Display_Text extends Application {
  @Override
  public void start(Stage Demo_Stage) {
    String Content = "DelftStack is a resource for everyone interested in programming, "
        + "embedded software, and electronics. It covers the programming languages "
        + "like Python, C/C++, C#, and so on in this website's first development stage. "
        + "Open-source hardware also falls in the website's scope, like Arduino, "
        + "Raspberry Pi, and BeagleBone. DelftStack aims to provide tutorials, "
        + "how-to's, and cheat sheets to different levels of developers and hobbyists..";
    // Create a Label
    Label Demo_Text = new Label(Content);
    // wrap the label
    Demo_Text.setWrapText(true);
    // Set the maximum width of the label
    Demo_Text.setMaxWidth(300);
    // Set the position of the label
    Demo_Text.setTranslateX(30);
    Demo_Text.setTranslateY(30);
    Group Text_Root = new Group();
    Text_Root.getChildren().add(Demo_Text);
    // Set the stage
    Scene Text_Scene = new Scene(Text_Root, 595, 150, Color.BEIGE);
    Demo_Stage.setTitle("Display Multiline Text");
    Demo_Stage.setScene(Text_Scene);
    Demo_Stage.show();
  }
  public static void main(String args[]) {
    launch(args);
  }
}

上面的代码会将标签中包含的文本显示为多行。

输出:

JavaFX 显示多行文本