多读书多实践,勤思考善领悟

JavaFX Text(文本)

本文于1665天之前发表,文中内容可能已经过时。

就像各种形状一样,您也可以在JavaFX中创建一个文本节点。文本节点由名为Text的类表示,该类属于包javafx.scene.text

该类包含几个在JavaFX中创建文本并修改其外观的属性。该类还继承属于包javafx.scene.shape的Shape类。

因此,除了文本的属性,如字体,对齐,行间距,文本等。它还继承了基本的形状节点属性,如strokeFill,stroke,strokeWidth,strokeType等。

创建文本节点

由于包javafx.scene.text的类Text 表示JavaFX中的文本节点,因此可以通过实例化此类来创建文本,如下所示 -

1
Text text = new Text();

Text类包含一个名为text of string type 的属性,它表示要创建的文本。

实例化Text类之后,需要使用setText()方法为此属性设置值,如下所示。

1
2
String text = "Hello how are you" 
Text.setText(text);

您还可以通过使用各自的setter方法(即setX()setY()指定属性x和y的值来设置文本的位置(原点),如下面的代码块所示 -

1
2
text.setX(50); 
text.setY(50);

以下程序是演示如何在JavaFX中创建文本节点的示例。将此代码保存在名为TextExample.java的文件中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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文件。

1
2
javac TextExample.java 
java TextExample

执行时,上述程序生成一个显示指定文本的JavaFX窗口,如下所示 -

文本的位置和字体

默认情况下,文本类创建的文本具有字体…,大小…和黑色。

您可以使用setFont()方法更改文本的字体大小和颜色。此方法接受Font类的对象。

包名为Fontjavafx.scene.text包用于定义文本的字体。该类包含一个名为font()的静态方法。

该方法接受四个参数,即 -

  • family - 这是String类型,表示我们要应用于文本的字体系列。
  • weight - 此属性表示字体的权重。它接受9个值,分别是 - FontWeight.BLACK,FontWeight.BOLD,FontWeight.EXTRA_BOLD,FontWeight.EXTRA_LIGHT,LIGHT,MEDIUM,NORMAL,SEMI_BOLD,THIN
  • pose - 此属性表示字体状态(常规或斜体)。它接受两个值FontPosture.REGULARFontPosture.ITALIC
  • size - 此属性的类型为double,它表示字体的大小。

您可以使用以下方法将字体设置为文本 -

1
text.setFont(Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 20));

以下程序是演示如何在JavaFX中设置文本节点字体的示例。在这里,我们将字体设置为Verdana,重量设置为粗体,姿势设置为常规,大小设置为20。

将此代码保存在名为TextFontExample.java的文件中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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文件。

1
2
javac TextFontExample.java 
java TextFontExample

执行时,上述程序生成一个JavaFX窗口,显示具有指定字体的文本,如下所示 -

笔划和颜色

Text类还继承了包的类Shape。因此,您可以使用javafx.scene.shape,您也可以使用它设置笔划和颜色到文本节点。

您可以使用shape(inherited)类的setFill()方法将颜色设置为文本,如下所示 -

1
text.setFill(Color.BEIGE);

同样,您可以使用setStroke()方法设置文本的笔触颜色。虽然可以使用方法setStrokeWidth()设置笔划的宽度,如下所示 -

1
2
3
4
5
6
7
8
//Setting the color 
text.setFill(Color.BROWN);

//Setting the Stroke
text.setStrokeWidth(2);

//Setting the stroke color
text.setStroke(Color.BLUE);

以下程序是演示如何设置文本节点的颜色,strokeWidth和strokeColor的示例。在此代码中,我们将笔触颜色设置为 - 蓝色,文本颜色设置为 - 棕色,笔触宽度设置为-2。

将此代码保存在名为StrokeExample.java的文件中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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.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 color
text.setFill(Color.BROWN);

//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文件。

1
2
javac StrokeExample.java 
java StrokeExample

执行时,上述程序生成一个JavaFX窗口,显示具有指定笔划和颜色属性的文本,如下所示 -

将装饰应用于文本

你也可以应用穿透等装饰; 在这种情况下,一行将通过文本传递。您可以使用Text类的方法为文本加下划线。

您可以使用方法setStrikethrough()来浏览文本。这接受一个布尔值,将值true传递给此方法以触发文本,如下面的代码框所示 -

1
2
//Striking through the text 
text1.setStrikethrough(true);

以同样的方式,您可以通过将值true传递给方法setUnderLine()来为文本加下划线,如下所示 -

1
2
//underlining the text     
text2.setUnderline(true);

以下程序是一个示例,演示如何将装饰(如下划线穿透)应用于文本。将此代码保存在名为DecorationsExample.java的文件中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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 DecorationsExample extends Application {
@Override
public void start(Stage stage) {
//Creating a Text_Example object
Text text1 = new Text("Hi how are you");

//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);

//Striking through the text
text1.setStrikethrough(true);

//Creating a Text_Example object
Text text2 = new Text("Welcome to Tutorialspoint");

//Setting font to the text
text2.setFont(Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 20));

//setting the position of the text
text2.setX(50);
text2.setY(150);

//underlining the text
text2.setUnderline(true);

//Creating a Group object
Group root = new Group(text1, text2);

//Creating a scene object
Scene scene = new Scene(root, 600, 300);

//Setting title to the Stage
stage.setTitle("Decorations 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文件。

1
2
javac DecorationsExample.java 
java DecorationsExample

执行时,上面的程序生成一个JavaFX窗口,如下所示 -