Java教程-JavaFX文本

JavaFX文本
在某些情况下,我们需要在应用程序界面上提供基于文本的信息。JavaFX库提供了一个名为javafx.scene.text.Text的类来实现此目的。该类提供了各种方法来修改文本的各种属性。我们只需实例化这个类就可以在应用程序中实现文本。
属性
JavaFX Text的属性如下表所述。
Property | Description | Setter Methods |
---|---|---|
boundstype | 此属性是对象类型。它决定了计算文本边界的方式。 | setBoundsType(TextBoundsType value) |
font | 文本的字体。 | setFont(Font value) |
fontsmoothingType | 定义请求的字体平滑类型。 | setFontSmoothingType(FontSmoothingType value) |
linespacing | 行与行之间的垂直间距(以像素为单位)。它是双重类型的财产。 | setLineSpacing(double spacing) |
strikethrough | 这是一个布尔类型的属性。我们可以通过将此属性设置为 true 来在文本中画一条线。 | setStrikeThrough(boolean value) |
textalignment | 水平文本对齐 | setTextAlignment(TextAlignment value) |
textorigin | 本地坐标系中文本坐标系的原点 | setTextOrigin(VPos value) |
text | 它是一个字符串类型的属性。它定义要显示的文本字符串。 | setText(String value) |
underline | 它是一个布尔类型的属性。我们可以通过将此属性设置为 true 来为文本加下划线。 | setUnderLine(boolean value) |
wrappingwidth | 文本要换行的文本宽度限制。这是一个双重类型的属性。 | setWrappingWidth(double value) |
x | 文本的 X 坐标 | setX(double value) |
y | 文本的 Y 坐标 | setY(double value) |
创建文本节点
为了创建文本节点,需要实例化javafx.scene.text.Text类。使用setter方法setText(string)将字符串设置为文本类对象的文本。按照下面给出的语法来实例化Text类。
Text <text_Object> = new Text();
text.setText(<string-text>);
示例
下面的示例说明了Text类。在这里,我们没有为文本设置位置,因此文本将显示在屏幕中央。
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class TextExample extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
Text text = new Text();
text.setText("Hello !! Welcome to JavaTPoint");
StackPane root = new StackPane();
Scene scene = new Scene(root,300,400);
root.getChildren().add(text);
primaryStage.setScene(scene);
primaryStage.setTitle("Text Example");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
文本的字体和位置
JavaFX允许我们为文本节点应用各种字体。我们只需通过使用setFont()方法设置Text类的font属性即可。该方法接受Font类的对象。Font类属于javafx.scene.text包。它包含一个名为font()的静态方法。该方法返回一个Font类型的对象,该对象将作为参数传递给Text类的setFont()方法。方法Font.font()接受以下参数。
- Family:表示字体的族群。它是字符串类型,应该是系统中存在的合适的字体族群。
- Weight:这是Font类的权重属性。有9个值可以用作字体的权重。这些值是FontWeight.BLACK、BOLD、EXTRA_BOLD、EXTRA_LIGHT、LIGHT、MEDIUM、NORMAL、SEMI_BOLD、THIN。
- Posture:这个Font类属性表示字体的姿势。它可以是FontPosture.ITALIC或FontPosture.REGULAR。
- Size:这是一个double类型的属性。用于设置字体的大小。
方法setFont()的语法如下。
<text_object>.setFont(Font.font(<String font_family>, <FontWeight>, <FontPosture>, <FontSize>)
示例
package application;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class TextExample extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
Text text = new Text();
text.setX(100);
text.setY(20);
text.setFont(Font.font("Abyssinica SIL",FontWeight.BOLD,FontPosture.REGULAR,20));
text.setText("Welcome to JavaTPoint");
Group root = new Group();
Scene scene = new Scene(root,500,200);
root.getChildren().add(text);
primaryStage.setScene(scene);
primaryStage.setTitle("Text Example");
primaryStage.show();
}
publicstaticvoid main(String[] args) {
launch(args);
}
}
应用描边和颜色到文本
描边是文本边界处的填充。JavaFX允许我们对文本应用描边和颜色。javafx.scene.text.Text类提供了一个名为setStroke()的方法,它接受Paint类对象作为参数。只需传递要应用的颜色来绘制描边。我们还可以通过将double类型的宽度值传递给setStrokeWidth()方法来设置描边的宽度。要设置文本的颜色,javafx.scene.text.Text类还提供了另一个名为setFill()的方法。我们只需传递要填充到文本中的颜色。
示例
下面的示例说明了上述方法的功能。
package application;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
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;
import javafx.stage.Stage;
public class TextExample extends Application{
@Override
publicvoid start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
Text text = new Text();
text.setX(100);
text.setY(20);
text.setFont(Font.font("Abyssinica SIL",FontWeight.BOLD,FontPosture.REGULAR,25));
text.setFill(Color.BLUE);// setting colour of the text to blue
text.setStroke(Color.BLACK); // setting the stroke for the text
text.setStrokeWidth(1); // setting stroke width to 2
text.setText("Welcome to JavaTPoint");
Group root = new Group();
Scene scene = new Scene(root,500,200);
root.getChildren().add(text);
primaryStage.setScene(scene);
primaryStage.setTitle("Text Example");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
应用装饰到文本
我们可以通过设置javafx.scene.text.Text类的strikethrough和underline属性来对文本应用装饰。两个方法的语法如下。
<TextObject>.setStrikeThrough(Boolean value) //传递 true 以在文本中划线
<TextObject>.setUnderLine(Boolean value) //传递 true 以在文本下划线
我们还可以对Text类对象应用JavaFX效果。我们将在接下来的章节中讨论JavaFX效果。
示例
package application;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class TextExample extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
Text text = new Text();
text.setX(100);
text.setY(40);
text.setFont(Font.font("Liberation Serif",25));
text.setText("Hello !!");
text.setStrikethrough(true);
Text text1=new Text();
text1.setX(100);
text1.setY(140);
text1.setText("Welcome to JavaTPoint!");
text1.setFont(Font.font("Liberation Serif",25));
text1.setUnderline(true);
Group root = new Group();
Scene scene = new Scene(root,500,200);
root.getChildren().addAll(text,text1);
primaryStage.setScene(scene);
primaryStage.setTitle("Text Example");
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}