如何在JavaFX中使文本变为粗体和斜体?
的字体类表示JavaFX的字体,这类提供了一种方法命名的几个变体的字体()如下所示-
font(double size) font(String family) font(String family, double size) font(String family, FontPosture posture, double size) font(String family, FontWeight weight, double size) font(String family, FontWeight weight, FontPosture posture, double size)
哪里,
size(双精度)表示字体的大小。
family(字符串)表示我们要应用于文本的字体的系列。您可以使用getFamilies()方法获取已安装字体系列的名称。
weight表示字体的权重(FontWeight枚举的常量之一-BLACK,BOLD,EXTRA_BOLD,EXTRA_LIGHT,LIGHT,MEDIUM,NORMAL,SEMI_BOLD,THIN)。
位姿代表字体位姿(FontPosture枚举的常量之一:REGULAR,ITALIC)。
若要使文本变为粗体,请绕过FontWeight.BOLD或FontWeight.EXTRA_BOLD作为参数粗细的值,并通过将FontPosture.ITALIC作为参数斜体的值来使文本斜体,来创建一种字体。
示例
import java.io.FileNotFoundException;
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 Bold_Italic extends Application {
public void start(Stage stage) throws FileNotFoundException {
//创建一个文本对象
String str = "Welcome to Nhooo";
Text text = new Text(30.0, 80.0, str);
//将字体设置为粗体和斜体
Font font = Font.font("Verdana", FontWeight.BOLD, FontPosture.ITALIC, 35);
text.setFont(font);
//设置文字的颜色
text.setFill(Color.DARKCYAN);
//设置舞台
Group root = new Group(text);
Scene scene = new Scene(root, 595, 150, Color.BEIGE);
stage.setTitle("Bold And Italic");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
}输出结果
热门推荐
1 狗狗下楼训练
3 狗狗训练头盔
6 狗狗训练机构
7 训练狗狗步骤
9 狗狗能训练
10 狗狗基本动作训练方法
11 怎么训练狗狗叼饭盆
12 狗狗一岁了还能训练吗
13 训练狗狗的
14 宁波狗狗训练
15 训练狗狗拉屎
16 教狗狗训练
17 狗狗要怎么训练上厕所
18 狗狗靠的训练