如何在 JavaFX 中设置文本节点的字体?


在 JavaFX 中,文本节点由 **javafx.scene.text.Text** 类表示。默认情况下,JavaFX 创建的文本如下所示:

设置文本节点的所需字体

您可以使用 **setFont()** 方法在 JavaFX 中设置文本节点的所需字体。此方法接受 **javafx.scene.text.Font** 类的对象。

**Font** 类表示 JavaFX 中的字体,此类提供名为 **font()** 方法的几种变体,如下所示:

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**(double) 表示字体的尺寸。

  • **family**(string) 表示要应用于文本的字体的族名。您可以使用 *getFamilies()* 方法获取已安装字体族名的列表。

  • **weight** 表示字体的粗细(FontWeight 枚举的常量之一:BLACK、BOLD、EXTRA_BOLD、EXTRA_LIGHT、LIGHT、MEDIUM、NORMAL、SEMI_BOLD、THIN)。

  • **posture** 表示字体的样式(FontPosture 枚举的常量之一:REGULAR、ITALIC)。

所有这些方法都是静态方法,并返回一个 Font 对象。因此,要将字体设置为文本节点:

  • 实例化 Text 类。

  • 使用 setter 方法设置基本属性(如位置和文本字符串),或者将它们作为参数传递给构造函数。

  • 使用其中一个 font() 方法创建 Font 对象。

  • 使用 setFont() 方法将创建的字体设置为文本。

  • 将创建的节点添加到 Group 对象。

示例

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Scanner;
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 SettingFont extends Application {
   public void start(Stage stage) throws FileNotFoundException {
      //Reading the contents of a text file.
      InputStream inputStream = new FileInputStream("D:\sample.txt");
      Scanner sc = new Scanner(inputStream);
      StringBuffer sb = new StringBuffer();
      while(sc.hasNext()) {
         sb.append(" "+sc.nextLine()+"\n");
      }
      String str = sb.toString();
      //Creating a text object
      Text text = new Text();
      //Setting the basic properties of text
      text.setText(str);
      text.setX(10.0);
      text.setY(25.0);
      text.setWrappingWidth(580);
      //Creating the font object
      String font_name = Font.getFamilies().get(25);
      System.out.println("Font Name:"+font_name);
      int size = 25;
      Font font = Font.font(font_name, FontWeight.BOLD, FontPosture.REGULAR, size);
      //Setting font to the text
      text.setFont(font);
      //Setting the stage
      Group root = new Group(text);
      Scene scene = new Scene(root, 595, 300, Color.BEIGE);
      stage.setTitle("Displaying Text");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

sample.txt

假设 sample.txt 文件的内容如下:

JavaFX is a Java library used to build Rich Internet Applications. The applications written using this library can run consistently across multiple platforms. The applications developed using JavaFX can run on various devices such as Desktop Computers, Mobile Phones, TVs, Tablets, etc..
To develop GUI Applications using Java programming language, the programmers rely on libraries such as Advanced Windowing Tool kit and Swing. After the advent of JavaFX, these Java programmers can now develop GUI applications effectively with rich content.

输出

Font Name: Brush Script MT

它还会生成以下窗口:

更新于:2020年4月14日

1K+ 次查看

启动您的 职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.