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 −

更新于: 14-Apr-2020

267 次浏览

开创你的职业

通过完成课程获得认证

开始学习
广告