Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Text origin trong JavaFX là gì?


Ngoài hệ thống tọa độ cục bộ để định vị các nút, JavaFX cung cấp một hệ thống tọa độ bổ sung cho nút văn bản.

Thuộc tính textOrigin chỉ định gốc tọa độ của nút văn bản trong hệ thống tọa độ cha. Bạn có thể đặt giá trị cho thuộc tính này bằng phương thức setTextOrigin(). Phương thức này chấp nhận một trong các hằng số của enum VPos. Enum này chứa 4 hằng số: BASELINE, BOTTOM, CENTER và TOP.

Ví dụ

import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.InputStream; 
import java.util.Scanner; 
import javafx.application.Application; 
import javafx.geometry.VPos; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.paint.Color; 
import javafx.stage.Stage; 
import javafx.scene.text.Text; 
public class TextOriginExample extends Application { 
  public void start(Stage stage) throws FileNotFoundException { 
    // Đọc nội dung file văn bản
    InputStream inputStream = new FileInputStream("D:\\sample_text.txt"); 
    Scanner sc = new Scanner(inputStream); 
    StringBuffer sb = new StringBuffer(); 
    while(sc.hasNext()) { 
      sb.append(" " + sc.nextLine() + "\n"); 
    } 
    // Tạo đối tượng Text
    Text text = new Text(10.0, 25.0, sb.toString()); 
    // Bọc văn bản
    text.setWrappingWidth(565); 
    // Thiết lập vị trí dọc
    text.setTextOrigin(VPos.TOP); 
    // Thiết lập Stage
    Group root = new Group(text); 
    Scene scene = new Scene(root, 595, 150, Color.BEIGE); 
    stage.setTitle("Text Origin (TOP)"); 
    stage.setScene(scene); 
    stage.show(); 
  } 
  public static void main(String args[]){ 
    launch(args); 
  } 
}

sample.txt

Giả sử nội dung file sample.txt như sau:

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.

Kết quả

Text origin trong JavaFX là gì?

Tương tự, nếu thay đổi giá trị căn chỉnh, bạn sẽ nhận được kết quả tương ứng:

BASELINE -

Text origin trong JavaFX là gì?

BOTTOM -

Text origin trong JavaFX là gì?

CENTER -

Text origin trong JavaFX là gì?