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

JavaFX에서 텍스트 정렬을 조정하는 방법은 무엇입니까?


값을 wrappingWidth로 설정하여 사용자 공간의 텍스트에 대해 고정 너비를 설정할 수 있습니다. 특성. 이렇게 하면 주어진 너비를 사용자 좌표에서 텍스트의 경계로 간주하여 텍스트가 주어진 너비만큼 너비로 정렬됩니다.

이 속성에 값을 지정하지 않은 경우 기본적으로 텍스트에서 가장 긴 줄의 길이가 경계 상자의 너비로 간주됩니다.

텍스트 정렬은 경계 상자 내에서 텍스트를 수평으로 정렬하는 것입니다. setTextAlignment()를 사용하여 텍스트 정렬을 조정할 수 있습니다. 방법. 이 메서드는 TextAlignment라는 열거형의 상수 중 하나를 허용합니다. 그에 따라 텍스트를 조정합니다. 이 열거형은 3개의 상수를 제공합니다 -

  • 중앙 − 테두리 상자의 중앙에 텍스트를 정렬합니다.

  • 정당화 − 경계 상자 내에서 텍스트 정렬을 정렬합니다.

  • 왼쪽 − 텍스트를 왼쪽으로 정렬합니다.

  • 오른쪽 − 텍스트를 오른쪽으로 정렬합니다.

예시

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.Text;
import javafx.scene.text.TextAlignment;
public class TextAllignment 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");
      }
      //Creating a text object
      Text text = new Text(10.0, 25.0, sb.toString());
      //Wrapping the text
      text.setWrappingWidth(565);
      //Setting the alignment
      text.setTextAlignment(TextAlignment.Right);
      //Setting the stage
      Group root = new Group(text);
      Scene scene = new Scene(root, 595, 150, Color.BEIGE);
      stage.setTitle("Text Alignment");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

샘플.txt

다음은 sample.txt 파일의 내용이라고 가정합니다. -

Tutorials Point originated from the idea that there exists a class of readers who respond better 
to online content and prefer to learn new skills at their own pace from the comforts of their drawing rooms.
The journey commenced with a single tutorial on HTML in 2006 and elated by the response it generated, 
we worked our way to adding fresh tutorials to our repository which now proudly flaunts a wealth of 
tutorials and allied articles on topics ranging from programming languages to web designing to academics 
and much more.

출력

JavaFX에서 텍스트 정렬을 조정하는 방법은 무엇입니까?

같은 방식으로 정렬 값을 변경하면 -

왼쪽 -

JavaFX에서 텍스트 정렬을 조정하는 방법은 무엇입니까?

중앙 -

JavaFX에서 텍스트 정렬을 조정하는 방법은 무엇입니까?

정당화 -

JavaFX에서 텍스트 정렬을 조정하는 방법은 무엇입니까?