Computer >> 컴퓨터 >  >> 프로그램 작성 >> Java

JavaFX에서 글꼴을 텍스트 노드로 설정하는 방법은 무엇입니까?


JavaFX에서 텍스트 노드는 javafx.scene.text.Text 수업. 기본적으로 JavaFX에 의해 생성된 텍스트는 다음과 같습니다 -

JavaFX에서 글꼴을 텍스트 노드로 설정하는 방법은 무엇입니까?

원하는 글꼴을 텍스트 노드로 설정

setFont()를 사용하여 JavaFX의 텍스트 노드에 원하는 글꼴을 설정할 수 있습니다. 방법. 이 메소드는 javafx.scene.text.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)

어디,

  • 크기 (이중)은 글꼴의 크기를 나타냅니다.

  • 가족 (문자열)은 텍스트에 적용하려는 글꼴 패밀리를 나타냅니다. getFamilies를 사용하여 설치된 글꼴 모음의 이름을 가져올 수 있습니다. () 메서드.

  • 무게 글꼴의 무게를 나타냅니다(FontWeight Enum의 상수 중 하나:BLACK, BOLD, EXTRA_BOLD, EXTRA_LIGHT, LIGHT, MEDIUM, NORMAL, SEMI_BOLD, THIN).

  • 자세 글꼴 자세를 나타냅니다(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);
   }
}

샘플.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

또한 다음 창을 생성합니다 -

JavaFX에서 글꼴을 텍스트 노드로 설정하는 방법은 무엇입니까?