如何調整JavaFX中Text節點中的行間距?
javafx.scene.text.Text類的行間距屬性指定文字(節點)各行之間的行間距(垂直)。
可以使用setLineSpacing()方法為此屬性設定值。此方法接受一個布林值作為引數,並設定行之間指定的空間(垂直)。
示例
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 TextSpacing 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 alignment
text.setTextAlignment(TextAlignment.JUSTIFY);
//Setting the space
text.setLineSpacing(2.0);
//Setting the stage
Group root = new Group(text);
Scene scene = new Scene(root, 595, 150, Color.BEIGE);
stage.setTitle("Line Spacing (2.0)");
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.
輸出
同樣,如果將行間距設定為8.0,則會出現以下輸出 −

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