JavaFX 中文字原點的含義是什麼?
除了用於定位其節點的區域性座標系外,JavaFX 還為文字節點提供了額外的座標系。
textOrigin 屬性指定父座標系中文字節點座標的原點。你可以使用 setTextOrigin() 方法為該屬性設定值。此方法接受列舉值 VPos 中的一個常量。此列舉包含 4 個常量,即:BASELINE、BOTTOM、CENTER 和 TOP。
示例
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 {
//Reading the contents of a text file.
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");
}
//Creating a text object
Text text = new Text(10.0, 25.0, sb.toString());
//Wrapping the text
text.setWrappingWidth(565);
//Setting the vertical positioning
text.setTextOrigin(VPos.TOP);
//Setting the 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
假設以下內容為 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.
輸出

同樣,如果你更改對齊值,你將獲得相應的結果,如下所示 −
BASELINE −

BOTTOM −

CENTER −

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP