个性化阅读
专注于IT技术分析

JavaFX文字

点击下载

本文概述

在某些情况下, 我们需要在应用程序界面上提供基于文本的信息。 JavaFX库为此提供了一个名为javafx.scene.text.Text的类。此类提供了多种方法来更改文本的各种属性。我们只需要实例化此类即可在应用程序中实现文本。

物产

下表描述了JavaFX Text的属性。

属性 描述 设置方法
boundstype 此属性是对象类型。它确定了计算文本边界的方式。 setBoundsType(TextBoundsType value)
font 文字的字体。 setFont(Font value)
fontsmoothingType 定义请求的字体平滑类型。 setFontSmoothingType(FontSmoothingType value)
linespacing 线之间的垂直间隔(以像素为单位)。它是double类型的属性。 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 srcmini");
	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文字

文字的字体和位置

JavaFX使我们能够将各种字体应用于文本节点。我们只需要使用setter方法setFont()设置Text类的属性字体。此方法接受Font类的对象。 Font类属于包javafx.scene.text。它包含一个名为font()的静态方法。这将返回一个Font类型的对象, 该对象将作为参数传递给Text类的setFont()方法。方法Font.font()接受以下参数。

  1. 家族:它代表字体的家族。它是字符串类型, 应该是系统中存在的适当字体系列。
  2. 权重:此Font类属性用于字体的权重。有9个值可以用作字体粗细。值是FontWeight.BLACK, BOLD, EXTRA_BOLD, EXTRA_LIGHT, LIGHT, MEDIUM, NORMAL, SEMI_BOLD, THIN。
  3. 姿势:此Font类属性表示字体的姿势。它可以是FontPosture.ITALIC或FontPosture.REGULAR。
  4. 大小:这是一个双精度型属性。用于设置字体大小。

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 srcmini");
	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文字2

将笔触和颜色应用于文本

笔触是指在文本边界处的填充。 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 srcmini"); 
	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文字3

将装饰应用于文本

我们可以通过设置javafx.scene.text.Text类的属性删除线和下划线来将修饰应用于文本。这两种方法的语法如下。

<TextObject>.setStrikeThrough(Boolean value) //pass true to put a line across the text
<TextObject>.setUnderLine(Boolean value) //pass true to underline the text

我们还可以将JavaFX Effects应用于Text类对象。我们将在接下来的章节中讨论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 srcmini!");
	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);
	
}
}
JavaFX文字4
赞(0)
未经允许不得转载:srcmini » JavaFX文字

相关推荐

评论 抢沙发

评论前必须登录!