就像各种形状一样,您也可以在JavaFX中创建一个文本节点.文本节点由名为 Text 的类表示,该类属于包 javafx.scene.text .
此类包含几个在JavaFX中创建文本并修改其外观的属性.这个类还继承了属于包 javafx.scene.shape 的Shape类.
因此,除了像font一样的文本属性外,对齐它还继承了基本的形状节点属性,如 strokeFill,stroke,strokeWidth,strokeType,等.
由于包 javafx.scene.text 的类Text表示JavaFX中的文本节点,因此可以通过实例化此类来创建文本,如下所示 :
Text text = new Text();
类Text包含一个名为 text 的属性,字符串类型,表示要创建的文本.
在实例化Text类之后,您需要使用 setText()方法为此属性设置值,如下所示.
String text = "Hello how are you" Text.setText(text);
您还可以通过使用各自的setter方法指定属性x和y的值来设置文本的位置(原点),即 setX ()和 setY(),如下面的代码块 :
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 : 这是String类型,表示我们要应用于文本的字体系列.
weight : 此属性表示字体的权重.它接受9个值,即 : FontWeight.BLACK,FontWeight.BOLD,FontWeight.EXTRA_BOLD,FontWeight.EXTRA_LIGHT,LIGHT,MEDIUM,NORMAL,SEMI_BOLD,THIN .
posture : 此属性表示字体状态(常规或斜体).它接受两个值 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 来设置文本节点的笔触和颜色.
您可以将颜色设置为使用形状(继承)类的 setFill()方法的文本如下 :
text.setFill( Color.BEIGE);
同样,您可以使用方法 setStroke()设置文本的笔触颜色.虽然可以使用方法 setStrokeWidth()设置笔划的宽度,如下所示 :
//设置颜色 text.setFill(Color.BROWN); //设置笔划 text.setStrokeWidth(2); //设置笔触颜色 text.setStroke(Color.BLUE);
以下程序演示了如何设置颜色,strokeWidth和strokeColor的示例文本节点.在此代码中,我们将笔触颜色设置为 - 蓝色,文本颜色设置为 - 棕色,笔触宽度设置为 - 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窗口,显示具有指定笔划和颜色属性的文本,如下所示;
您还可以应用装饰,例如穿透;在这种情况下,一行将通过文本传递.您可以使用文本类的方法为文本加下划线.
您可以使用方法 setStrikethrough()来浏览文本.这接受一个布尔值,将值 true 传递给此方法以触发文本,如下面的代码框所示 :
//浏览文本 text1.setStrikethrough(true);
以同样的方式,您可以通过将值 true 传递给方法 setUnderLine()
//为文本加下划线 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窗口,如下所示 :