迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

JavaFX 显示文本

作者:迹忆客 最近更新:2023/09/17 浏览次数:

可以使用 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 中创建和显示文本:

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

示例代码:

package delftstack;

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

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 jiyik.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

 

我们可以使用 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 = "Jiyik 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);
    }
}

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

 

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

如何在 Java 中延迟几秒钟的时间

发布时间:2023/12/17 浏览次数:217 分类:Java

本篇文章主要介绍如何在 Java 中制造程序延迟。本教程介绍了如何在 Java 中制造程序延时,并列举了一些示例代码来了解它。

如何在 Java 中把 Hashmap 转换为 JSON 对象

发布时间:2023/12/17 浏览次数:187 分类:Java

它描述了允许我们将哈希图转换为简单的 JSON 对象的方法。本文介绍了在 Java 中把 Hashmap 转换为 JSON 对象的方法。我们将看到关于创建一个 hashmap,然后将其转换为 JSON 对象的详细例子。

如何在 Java 中按值排序 Map

发布时间:2023/12/17 浏览次数:171 分类:Java

本文介绍了如何在 Java 中按值对 Map 进行排序。本教程介绍了如何在 Java 中按值对 Map 进行排序,并列出了一些示例代码来理解它。

如何在 Java 中打印 HashMap

发布时间:2023/12/17 浏览次数:192 分类:Java

本帖介绍了如何在 Java 中打印 HashMap。本教程介绍了如何在 Java 中打印 HashMap 元素,还列举了一些示例代码来理解这个主题。

在 Java 中更新 Hashmap 的值

发布时间:2023/12/17 浏览次数:146 分类:Java

本文介绍了如何在 Java 中更新 HashMap 中的一个值。本文介绍了如何在 Java 中使用 HashMap 类中包含的两个方法-put() 和 replace() 更新 HashMap 中的值。

Java 中的 hashmap 和 map 之间的区别

发布时间:2023/12/17 浏览次数:79 分类:Java

本文介绍了 Java 中的 hashmap 和 map 接口之间的区别。本教程介绍了 Java 中 Map 和 HashMap 之间的主要区别。在 Java 中,Map 是用于以键值对存储数据的接口,

在 Java 中获取用户主目录

发布时间:2023/12/17 浏览次数:218 分类:Java

这篇文章向你展示了如何在 Java 中获取用户主目录。本教程介绍了如何在 Java 中获取用户主目录,并列出了一些示例代码以指导你完成该主题。

Java 中 size 和 length 的区别

发布时间:2023/12/17 浏览次数:179 分类:Java

这篇文章教你如何知道 Java 中大小和长度之间的区别。本教程介绍了 Java 中大小和长度之间的区别。我们还列出了一些示例代码以帮助你理解该主题。

Java 中的互斥锁

发布时间:2023/12/17 浏览次数:111 分类:Java

了解有关 Java 中互斥锁的一切,在计算机科学领域,互斥或互斥被称为并发控制的属性。每台计算机都使用称为线程的最小程序指令序列。有一次,计算机在一个线程上工作。为了更好地理解,

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便