Javafx 简明教程
JavaFX - Text
JavaFX 应用程序可以包含许多元素,包括所有类型的媒体,如图像、视频、GIF 和所有尺寸形状、文本等。这是为了提高用户对应用程序的体验质量。所有这些元素都由 JavaFX 场景图上的节点表示。
A JavaFX application can consist of a lot of elements including all kinds of media like images, videos, GIFs, and all dimensional shapes, text, etc. This is to improve the quality of user experience with the application. All these elements are represented by nodes on a JavaFX scene graph.
以前,我们已经学习了如何创建 2D 和 3D 形状。但您还可以在 JavaFX 应用程序中创建 Text 元素。Text 元素由一个单独的节点表示,可以根据其字体、大小、颜色和其他一些属性进行更改。
Previously, we have learned how to create both 2D and 3D shapes. But you can also create a Text element in JavaFX applications. The Text element is represented by a separate node and it can be altered with respect to its font, size, color, and some other properties.
在本章中,我们将学习如何使用 JavaFX 在应用程序上显示 Text 节点。
In this chapter, we will learn how to display a Text node on an application using JavaFX.
JavaFX Text Node
JavaFX 中的文本节点由名为 Text 的类表示,该类属于包 javafx.scene.text 。
The text node in JavaFX is represented by the class named Text, which belongs to the package javafx.scene.text.
此类包含用于在 JavaFX 中创建文本并修改其外观的多个属性。此类还继承属于包 javafx.scene.shape 的 Shape 类。
This class contains several properties to create text in JavaFX and modify its appearance. This class also inherits the Shape class which belongs to the package javafx.scene.shape.
因此,除了字体、对齐、行距、文本等文本属性外,它还继承了 strokeFill 、 stroke 、 strokeWidth 、 strokeType 等基本形状节点属性。
Therefore, in addition to the properties of the text like font, alignment, line spacing, text, etc. It also inherits the basic shape node properties such as strokeFill, stroke, strokeWidth, strokeType etc.
Creating a Text Node
由于包 javafx.scene.text 的 Text 类表示 JavaFX 中的文本节点,因此您可以通过如下所示实例化此类来创建文本 -
Since the class Text of the package javafx.scene.text represents the text node in JavaFX, you can create a text by instantiating this class as follows −
Text text = new Text();
Text 类包含一个名为 text 的字符串类型属性,它表示要创建的文本。
The class Text contains a property named text of string type, which represents the text that is to be created.
实例化 Text 类后,需要使用 setText() 方法为该属性设置值,如下所示。
After instantiating the Text class, you need to set value to this property using the setText() method as shown below.
String text = "Hello how are you"
Text.setText(text);
您还可以通过使用各自的设置器方法,即 setX() 和 setY() ,为 x 和 y 指定值来设置文本的位置(原点),如下面的代码块所示 -
You can also set the position (origin) of the text by specifying the values to the properties x and y using their respective setter methods namely setX() and setY() as shown in the following code block −
text.setX(50);
text.setY(50);
以下程序是一个示例,展示了如何在 JavaFX 中创建文本节点。将此代码保存在名为 TextExample.java 的文件中。
The following program is an example demonstrating how to create a text node in JavaFX. Save this code in a file with name TextExample.java.
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);
}
}
使用以下命令,从命令提示符编译并执行已保存的 java 文件。
Compile and execute the saved java file from the command prompt using the following commands.
javac --module-path %PATH_TO_FX% --add-modules javafx.controls TextExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls TextExample
执行时,上述程序会生成一个 JavaFX 窗口,其中显示指定的文本,如下所示 -
On executing, the above program generates a JavaFX window displaying the specified text as follows −
让我们看另一个示例,其中我们尝试通过对文本应用字体、大小、对齐等各种属性来创建文本节点。将此代码保存在名为 TextExample1.java 的文件中。
Let us see another example where we are trying to create a text node by applying various properties like Font, size, alignment, etc. on the said text. Save this code in a file with name 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);
}
}
使用以下命令,从命令提示符编译并执行已保存的 java 文件。
Compile and execute the saved java file from the command prompt using the following commands.
javac --module-path %PATH_TO_FX% --add-modules javafx.controls TextExample1.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls TextExample1
执行时,上述程序会生成一个 JavaFX 窗口,其中显示指定的文本,如下所示 -
On executing, the above program generates a JavaFX window displaying the specified text as follows −
Position & Font of a Text node
您还可以在 JavaFX 应用程序中添加一个文本节点。但是,任何可以添加的文本都有一些默认值,例如文本大小、字体及其颜色(为黑色)。但是,有必要更改默认值,因为它们并非适用于所有场景。
You can also add a Text Node in a JavaFX application. However, any text that can be added has some default values set to it, like the size of text, font, and its color (which is black). However, it becomes necessary to change the default values, as they are not suitable for all scenarios.
例如,JavaFX 应用程序中文本节点的默认位置从屏幕开头开始。但是,当文本内容较长并且超出显示范围时,就有必要更改其位置以正确显示所有内容。
For instance, the default position of a text node in JavaFX application starts from the beginning of the screen. But, when the text content is longer and it goes out of display range, there arises a need to change its position to display all the content properly.
更改文本的位置和字体还将允许用户根据自己的要求开发应用程序。
Changing the position and font of text will also allow the user to develop an application as per their own requirements.
The setFont() Method
您可以使用 setFont() 方法更改文本的字体大小和颜色。此方法接受 Font 类的对象。
You can change the font size and color of the text using the setFont() method. This method accepts an object of the Font class.
包 javafx.scene.text 中名为 Font 的类用于定义文本的字体。此类包含一个名为 font() 的静态方法。
The class named Font of the package javafx.scene.text is used to define the font for the text. This class contains a static method named font().
此方法接受四个参数,即:
This method accepts four parameters namely −
-
family − This is of a String type and represents the family of the font that we want to apply to the text.
-
weight − This property represents the weight of the font. It accepts 9 values, which are − FontWeight.BLACK, FontWeight.BOLD, FontWeight.EXTRA_BOLD, FontWeight.EXTRA_LIGHT, LIGHT, MEDIUM, NORMAL, SEMI_BOLD, THIN.
-
posture − This property represents the font posture (regular or italic). It accepts two values FontPosture.REGULAR and FontPosture.ITALIC.
-
size − This property is of type double and it represents the size of the font.
您可以使用以下方法将字体设置为文本 −
You can set font to the text by using the following method −
text.setFont(Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 20));
在尝试设置所需的文本位置和字体之前,让我们看看一个在 JavaFX 应用程序中具有文本节点默认属性的程序。
Before trying to set the desired position and font, let us see a program with default properties of a text node in a JavaFX application.
将此代码保存在一个名为 TextDefault.java 的文件中。
Save this code in a file with the name TextDefault.java.
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.text.*;
public class TextDefault extends Application {
@Override
public void start(Stage stage) {
//Creating a Text object
Text text = new Text();
//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("Default 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);
}
}
使用以下命令从命令提示符处编译并执行已保存的 Java 文件。
Compile and execute the saved Java file from the command prompt using the following commands.
javac --module-path %PATH_TO_FX% --add-modules javafx.controls TextDefault.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls TextDefault
执行后,上述程序会生成一个 JavaFX 窗口,显示具有其默认属性的文本。
On executing, the above program generates a JavaFX window displaying the text with its default properties.
正如您所看到的,文本在应用程序中没有正确显示,因此,需要设置位置和字体属性。
As you can see, the text is not correctly displayed within the application, hence, prompting the need for setting position and font properties.
以下程序是一个示例,展示了如何在 JavaFX 中设置文本节点的字体。在这里,我们设置字体为 Verdana,粗细为粗体,姿势为常规,大小为 20。
The following program is an example demonstrating how to set font of the text node in JavaFX. In here, we are setting the font to Verdana, weight to bold, posture to regular and size to 20.
将此代码保存在一个名为 TextFontExample.java 的文件中。
Save this code in a file with the name TextFontExample.java.
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);
}
}
使用以下命令,从命令提示符编译并执行已保存的 java 文件。
Compile and execute the saved java file from the command prompt using the following commands.
javac --module-path %PATH_TO_FX% --add-modules javafx.controls TextFontExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls TextFontExample
执行后,上述程序会生成一个 JavaFX 窗口,显示具有指定字体的文本,如下所示 −
On executing, the above program generates a JavaFX window displaying the text with the specified font as follows −
Stroke and Color of a Text node
JavaFX 中的每个节点都有一些默认值分配给它,这与它们的显示和放置方式有关。例如,任何像盒子、圆柱、球体等的 3D 形状都具有浅灰色的漫射颜色作为其默认颜色。
Every node in JavaFX has some default values assigned to it, in regards to the way they are displayed and positioned. For instance, any 3D shape like box, cylinder, sphere etc. has a diffuse color of light gray as its default color.
您还可以更改 JavaFX 文本节点的此类默认值。可以以多种方式设计文本节点:加下划线、加粗、斜体,可以使用双笔划或更宽的笔划书写文本等等。所有这些改进都可以通过 JavaFX 应用程序进行。
You can also change such default values of a JavaFX Text node. A Text node can be designed in various ways: underlined, bold, italic, the text can be written with either double strokes, or wider strokes, etc. All these improvements can be made with the JavaFX Application as well.
The setFill() Method
文本类还继承了包的形状类。因此,您可以使用 javafx.scene.shape 来为文本节点设置笔划和颜色。
The Text class also inherits the class Shape of the package. Therefore, you can use javafx.scene.shape with which you can set the stroke and color to the text node too.
您可以按照以下方式使用形状(继承)类的 setFill() 方法将颜色设置为文本 −
You can set the color to the text using the setFill() method of the shape (inherited) class as follows −
text.setFill(Color.BEIGE);
同样,您可以使用 setStroke() 方法设置文本的描边颜色。而描边的宽度可以使用 setStrokeWidth() 方法设置,如下所示 −
Similarly, you can set the stroke color of the text using the method setStroke(). While the width of the stroke can be set using the method setStrokeWidth() as follows −
//Setting the color
text.setFill(Color.BROWN);
//Setting the Stroke
text.setStrokeWidth(2);
//Setting the stroke color
text.setStroke(Color.BLUE);
下述程序是展示如何设置文本节点的 strokeWidth 的示例。在此代码中,我们设置笔触宽度为“2”。
The following program is an example that demonstrates how to set the strokeWidth of the text node. In this code, we are setting the stroke width to "2".
将此代码保存在一个名为 StrokeExample.java 的文件中。
Save this code in a file with the name StrokeExample.java.
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);
}
}
使用以下命令,从命令提示符编译并执行已保存的 java 文件。
Compile and execute the saved java file from the command prompt using the following commands.
javac --module-path %PATH_TO_FX% --add-modules javafx.controls StrokeExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls StrokeExample
执行上述程序时,会生成一个 JavaFX 窗口,其按如下方式显示具有指定描边和颜色属性的文本:
On executing, the above program generates a JavaFX window displaying the text with the specified stroke and color attributes as follows −
让我们尝试为 Save this code in a file with the name ColorExample.java 着色。
Let us try to color the Save this code in a file with the name ColorExample.java.
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.text.*;
public class ColorExample 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("Times New Roman", FontWeight.LIGHT, FontPosture.REGULAR, 20));
//setting the position of the text
text.setX(50);
text.setY(130);
//Setting the color
text.setFill(Color.BROWN);
//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);
}
}
使用以下命令,从命令提示符编译并执行已保存的 java 文件。
Compile and execute the saved java file from the command prompt using the following commands.
javac --module-path %PATH_TO_FX% --add-modules javafx.controls ColorExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls ColorExample
执行上述程序时,会生成一个 JavaFX 窗口,其按如下方式显示具有指定描边和颜色属性的文本:
On executing, the above program generates a JavaFX window displaying the text with the specified stroke and color attributes as follows −
Applying Decorations to a Text node
您还可以应用诸如删除线之类的装饰,在这种情况下,会在文本中穿插一条线,并使用 Text 类的某些方法对文本进行下划线处理。
You can also apply decorations such as strike through, in which case a line is passed through the text, and underlining a text using the methods of the Text class.
您可以使用 setStrikethrough() 方法在文本中加删除线。该方法接受布尔值,向该方法传递值 true 会在文本中加删除线,如以下代码框所示:
You can strike through the text using the method setStrikethrough(). This accepts a Boolean value, pass the value true to this method to strike through the text as shown in the following code box −
//Striking through the text
text1.setStrikethrough(true);
同理,您可以通过向方法 setUnderLine() 传递值 true 为文本添加下划线,如下所示:
In the same way, you can underline a text by passing the value true to the method setUnderLine() as follows −
//underlining the text
text2.setUnderline(true);
Example
以下程序提供了一个示例,演示如何向文本应用删除线装饰。将此代码保存在一个名为 StrikeThroughExample.java 的文件中。
The following program is an example demonstrating how to apply strike through decoration to a text. Save this code in a file with the name 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);
}
}
使用以下命令从命令提示符处编译并执行已保存的 Java 文件。
Compile and execute the saved Java file from the command prompt using the following commands.
javac --module-path %PATH_TO_FX% --add-modules javafx.controls StrikeThroughExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls StrikeThroughExample
执行上述程序时,会生成一个 JavaFX 窗口,如下所示:
On executing, the above program generates a JavaFX window as shown below −
Example
以下程序提供了一个示例,演示如何向文本应用下划线装饰。将此代码保存在一个名为 UnderlinesExample.java 的文件中。
The following program is an example demonstrating how to apply underline decoration to a text. Save this code in a file with the name UnderlinesExample.java.
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.text.*;
public class UnderlinesExample 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);
//underlining the text
text1.setUnderline(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("Underline 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);
}
}
使用以下命令从命令提示符处编译并执行已保存的 Java 文件。
Compile and execute the saved Java file from the command prompt using the following commands.
javac --module-path %PATH_TO_FX% --add-modules javafx.controls UnderlinesExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls UnderlinesExample
执行上述程序时,会生成一个 JavaFX 窗口,如下所示:
On executing, the above program generates a JavaFX window as shown below −