- JavaFX Tutorial
- JavaFX - Home
- JavaFX - Overview
- JavaFX - Environment
- JavaFX - Architecture
- JavaFX - Application
- JavaFX - 2D Shapes
- JavaFX - Text
- JavaFX - Effects
- JavaFX - Transformations
- JavaFX - Animations
- JavaFX - Colors
- JavaFX - Images
- JavaFX - 3D Shapes
- JavaFX - Event Handling
- JavaFX - UI Controls
- JavaFX - Charts
- JavaFX - Layout Panes
- JavaFX - CSS
- JavaFX Useful Resources
- JavaFX - Quick Guide
- JavaFX - Useful Resources
- JavaFX - Discussion
JavaFX - 文本
就像各种形状一样,您也可以在 JavaFX 中创建文本节点。文本节点由名为Text的类表示,该类属于包javafx.scene.text。
此类包含多个属性,用于在 JavaFX 中创建文本并修改其外观。该类还继承了属于javafx.scene.shape包的 Shape 类。
因此,除了文本的字体、对齐方式、行距、文本等属性外,它还继承了基本的形状节点属性,如描边填充、描边、描边宽度、描边类型等。
创建文本节点
由于javafx.scene.text包的 Text 类代表 JavaFX 中的文本节点,因此您可以通过实例化此类来创建文本,如下所示 -
Text text = new Text();
Text类包含一个名为text的字符串类型的属性,它表示要创建的文本。
实例化 Text 类后,您需要使用setText()方法为此属性设置值,如下所示。
String text = "Hello how are you" Text.setText(text);
您还可以通过使用各自的 setter 方法(即setX()和setY())指定属性 x 和 y 的值来设置文本的位置(原点),如以下代码块所示 -
text.setX(50); text.setY(50);
例子
以下程序是演示如何在 JavaFX 中创建文本节点的示例。将此代码保存在名为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 文件。
javac TextExample.java java TextExample
执行时,上述程序会生成一个 JavaFX 窗口,显示指定的文本,如下所示 -
文本的位置和字体
默认情况下,文本类创建的文本的字体...、大小...、颜色为黑色。
您可以使用setFont()方法更改文本的字体大小和颜色。此方法接受Font类的对象。
javafx.scene.text包中名为Font的类用于定义文本的字体。此类包含一个名为font()的静态方法。
该方法接受四个参数,即 -
family - 这是一个字符串类型,表示我们要应用于文本的字体系列。
重量- 该属性表示字体的重量。它接受 9 个值,分别是 - FontWeight.BLACK、FontWeight.BOLD、FontWeight.EXTRA_BOLD、FontWeight.EXTRA_LIGHT、LIGHT、MEDIUM、NORMAL、SEMI_BOLD、THIN。
姿势- 该属性表示字体姿势(常规或斜体)。它接受两个值FontPosture.REGULAR和FontPosture.ITALIC。
size - 该属性的类型为 double,它表示字体的大小。
您可以使用以下方法设置文本字体 -
text.setFont(Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 20));
例子
以下程序是演示如何在JavaFX中设置文本节点字体的示例。在这里,我们将字体设置为 Verdana,粗细设置为粗体,姿势设置为常规,大小设置为 20。
将此代码保存在名为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 文件。
javac TextFontExample.java java TextFontExample
执行时,上述程序会生成一个 JavaFX 窗口,显示具有指定字体的文本,如下所示 -
描边和颜色
Text 类还继承了包中的 Shape 类。因此,您可以使用javafx.scene.shape来设置文本节点的描边和颜色。
您可以使用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);
例子
下面的程序是一个示例,演示如何设置文本节点的颜色、描边宽度和描边颜色。在此代码中,我们将描边颜色设置为 – 蓝色,文本颜色设置为 – 棕色,描边宽度设置为 – 2。
将此代码保存在名为StrokeExample.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.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 文件。
javac StrokeExample.java java StrokeExample
执行时,上述程序会生成一个 JavaFX 窗口,显示具有指定笔划和颜色属性的文本,如下所示 -
将修饰应用于文本
您还可以应用装饰,例如删除线;在这种情况下,一行将穿过文本。您可以使用Text类的方法为文本添加下划线。
您可以使用setStrikethrough()方法删除文本。它接受一个布尔值,将值true传递给此方法以删除文本,如以下代码框所示 -
//Striking through the text text1.setStrikethrough(true);
以同样的方式,您可以通过将值true传递给方法setUnderLine()来为文本添加下划线,如下所示 -
//underlining the text text2.setUnderline(true);
例子
以下程序是演示如何对文本应用下划线或删除线等装饰的示例。将此代码保存在名为DecorationsExample.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 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 文件。
javac DecorationsExample.java java DecorationsExample
执行时,上述程序会生成一个 JavaFX 窗口,如下所示 -
