JavaFX 应用程序可以包含许多元素,包括各种媒体,如图像、视频、GIF 和所有维度的形状、文本等。这是为了提高应用程序的用户体验质量。所有这些元素都由 JavaFX 场景图上的节点表示。在本章中,我们将学习如何使用 JavaFX 在应用程序上显示 Text 节点。
JavaFX 中的文本节点由名为 Text 的类表示,该类属于包 javafx.scene.text 。
此类包含多个属性,用于在 JavaFX 中创建文本并修改其外观。此类还继承了属于 javafx.scene.shape 包的 Shape 类。
因此,除了字体、对齐方式、行距、文本等文本属性外。它还继承了基本的形状节点属性,例如 strokeFill 、 stroke 、 strokeWidth 、 strokeType 等。
由于包 javafx.scene.text 的类 Text 表示 JavaFX 中的文本节点,因此您可以通过实例化此类来创建文本,如下所示
Text text = new Text();
类 Text 包含一个名为 text 的字符串类型的属性,该属性表示要创建的文本。
实例化 Text 类后,需要使用 setText() 方法为此属性设置 value,如下所示。
String text = "Hello how are you"
Text.setText(text);
还可以通过使用各自的 setter 方法(即 setX() 和 setY))为属性 x 和 y 指定值来设置文本的位置(原点),如下面的代码块所示
text.setX(50);
text.setY(50);
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.text.Text;
public class TextExample extends Application {
@Override
public void start(Stage stage) {
//Creating a Text object
Text text = new Text();
//Setting the text to be added.
text.setText("Hello how are you");
//setting the position of the text
text.setX(50);
text.setY(50);
//Creating a Group object
Group root = new Group(text);
//Creating a scene object
Scene scene = new Scene(root, 600, 300);
//Setting title to the Stage
stage.setTitle("Sample Application");
//Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
让我们看另一个例子,我们尝试通过在所述文本上应用各种属性(如 Font、size、alignment 等)来创建文本节点。将此代码保存在名为 TextExample1.java 的文件中。
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.text.*;
public class TextExample1 extends Application {
@Override
public void start(Stage stage) {
//Creating a Text object
Text text = new Text();
text.setFont(new Font(20));
text.setWrappingWidth(200);
text.setTextAlignment(TextAlignment.JUSTIFY);
text.setText("This is Paragraph 1\nThis is Paragraph 2");
//setting the position of the text
text.setX(50);
text.setY(130);
//Creating a Group object
Group root = new Group(text);
//Creating a scene object
Scene scene = new Scene(root, 600, 300);
//Setting title to the Stage
stage.setTitle("Sample Application");
//Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
还可以在 JavaFX 应用程序中添加文本节点。但是,任何可以添加的文本都设置了一些默认值,例如文本的大小、字体及其颜色(黑色)。但是,有必要更改默认值,因为它们并不适合所有场景。
例如,JavaFX 应用程序中文本节点的默认位置从屏幕的开头开始。但是,当文本内容较长并且超出显示范围时,就需要更改其位置以正确显示所有内容。
更改文本的位置和字体还将允许用户根据自己的要求开发应用程序。
可以使用 setFont() 方法更改文本的字体大小和颜色。此方法接受 Font 类的对象。
程序包 javafx.scene.text 的名为 Font 的类用于定义文本的字体。此类包含一个名为 font() 的静态方法。此方法接受四个参数
可以使用以下方法将 font 设置为文本
text.setFont(Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 20));
以下程序是一个示例,演示如何在 JavaFX 中设置文本节点的字体。在这里,我们将字体设置为 Verdana,将 weight 设置为粗体,将 posture 设置为 regular,将 size 设置为 20。
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
public class TextFontExample extends Application {
@Override
public void start(Stage stage) {
//Creating a Text object
Text text = new Text();
//Setting font to the text
text.setFont(Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 20));
//setting the position of the text
text.setX(50);
text.setY(130);
//Setting the text to be added.
text.setText("Hi how are you");
//Creating a Group object
Group root = new Group(text);
//Creating a scene object
Scene scene = new Scene(root, 600, 300);
//Setting title to the Stage
stage.setTitle("Setting Font to the text");
//Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
JavaFX 中的每个节点都分配了一些关于它们的显示和定位方式的默认值。例如,任何 3D 形状(如长方体、圆柱体、球体等)的默认颜色为浅灰色。
还可以更改 JavaFX Text 节点的此类默认值。文本节点可以以多种方式设计:下划线、粗体、斜体,文本可以用双笔画或更宽的笔画书写,等等。所有这些改进也可以通过 JavaFX 应用程序进行。
Text 类还继承包的类 Shape。因此,也可以使用 javafx.scene.shape 来设置文本节点的描边和颜色。
可以使用形状(继承)类的 setFill() 方法为文本设置颜色,如下所示
text.setFill(Color.BEIGE);
同样,可以使用方法 setStroke() 设置文本的描边颜色。虽然可以使用 setStrokeWidth() 方法设置笔画的宽度,如下所示
//Setting the color
text.setFill(Color.BROWN);
//Setting the Stroke
text.setStrokeWidth(2);
//Setting the stroke color
text.setStroke(Color.BLUE);
以下程序是一个示例,演示如何设置 text 节点的 strokeWidth。在此代码中,我们将笔画宽度设置为 “2”。
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
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;
public class StrokeExample extends Application {
@Override
public void start(Stage stage) {
//Creating a Text object
Text text = new Text();
//Setting font to the text
text.setFont(Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 50));
//setting the position of the text
text.setX(50);
text.setY(130);
//Setting the Stroke
text.setStrokeWidth(2);
// Setting the stroke color
text.setStroke(Color.BLUE);
//Setting the text to be added.
text.setText("Hi how are you");
//Creating a Group object
Group root = new Group(text);
//Creating a scene object
Scene scene = new Scene(root, 600, 300);
//Setting title to the Stage
stage.setTitle("Setting font to the text");
//Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
还可以使用 Text 类的方法应用修饰,例如删除线(在这种情况下,在文本中传递一行)并为文本添加下划线。
可以使用方法 setStrikethrough() 删除文本。这接受一个布尔值,将值 true 传递给此方法以删除文本,如以下代码框所示
//Striking through the text
text1.setStrikethrough(true);
同样,可以通过将值 true 传递给方法 setUnderLine() 来为文本添加下划线,如下所示
//underlining the text
text2.setUnderline(true);
以下程序是一个示例,演示如何将删除线通过修饰应用于文本。将此代码保存在名为 StrikeThroughExample.java 的文件中。
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.text.*;
public class StrikeThroughExample extends Application {
@Override
public void start(Stage stage) {
//Creating a Text_Example object
Text text1 = new Text("Welcome to Tutorialspoint");
//Setting font to the text
text1.setFont(Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 20));
//setting the position of the text
text1.setX(50);
text1.setY(75);
//strike through the text
text1.setStrikethrough(true);
//Creating a Group object
Group root = new Group(text1);
//Creating a scene object
Scene scene = new Scene(root, 600, 300);
//Setting title to the Stage
stage.setTitle("Strike Through Decoration Example");
//Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
}
public static void main(String args[]){
launch(args);
}
}