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 中创建和显示文本:
- 通过扩展
Application
类并实现start()
方法来创建一个类。 - 通过实例化类
Text
创建文本。然后使用setX()
和setY()
方法设置x
和y
位置。 - 创建一个
组
类。 - 创建一个场景对象,实例化
scene
类,并将group
对象传递给scene
。 - 通过
setTitle
方法为舞台添加标题,并使用setScene()
方法将场景添加到舞台。 - 使用
show()
方法显示舞台并启动应用程序。
让我们根据上述步骤实现一个示例。
示例代码:
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 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
。
输出:
我们可以使用 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);
}
}
上面的代码会将标签中包含的文本显示为多行。
输出:
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布,任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站。本站所有源码与软件均为原作者提供,仅供学习和研究使用。如您对本站的相关版权有任何异议,或者认为侵犯了您的合法权益,请及时通知我们处理。