如何在 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.