另一个基本的JavaFX节点是Text节点,它允许我们在场景图上显示测试。

要创建 Text 节点,请使用 javafx.scene.text.Text 类。

所有JavaFX场景节点都从 javafx.scene.Node 扩展,并且它们继承了许多功能,例如缩放,翻译或旋转的功能。

Text节点的直接父对象是 javafx.scene.shape.Shape 类。我们可以在两个文本之间执行几何操作,如减法,相交或联合。您还可以使用文本剪辑视口区域。

import javafx.application.Application;import javafx.scene.Group;import javafx.scene.Scene;import javafx.scene.paint.Color;import javafx.scene.text.Text;import javafx.stage.Stage;public class Main extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Drawing Text"); Group root = new Group(); Scene scene = new Scene(root, 300, 250, Color.WHITE); int x = 100; int y = 100; int red = 30; int green = 40; int blue = 50; Text text = new Text(x, y, "JavaFX 2.0"); text.setFill(Color.rgb(red, green, blue, .99)); text.setRotate(60); root.getChildren().add(text);  primaryStage.setScene(scene); primaryStage.show(); }}

上面的代码生成以下结果。

旋转文本

旋转文本

import javafx.application.Application;import javafx.scene.Group;import javafx.scene.Scene;import javafx.scene.paint.Color;import javafx.scene.text.Text;import javafx.stage.Stage;public class Main extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Drawing Text"); Group root = new Group(); Scene scene = new Scene(root, 300, 250, Color.WHITE); int x = 100; int y = 100; int red = 30; int green = 40; int blue = 50; Text text = new Text(x, y, "JavaFX 2.0"); text.setFill(Color.rgb(red, green, blue, .99)); text.setRotate(60); root.getChildren().add(text);  primaryStage.setScene(scene); primaryStage.show(); }}

上面的代码生成以下结果。

文本字体

JavaFX的Font API使您能够更改字体样式和字体大小。

import javafx.application.Application;import javafx.scene.Group;import javafx.scene.Scene;import javafx.scene.effect.DropShadow;import javafx.scene.paint.Color;import javafx.scene.text.Font;import javafx.scene.text.FontWeight;import javafx.scene.text.Text;import javafx.stage.Stage;public class Main extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle(""); Group root = new Group(); Scene scene = new Scene(root, 300, 250, Color.WHITE); Group g = new Group(); Text t = new Text(); t.setCache(true); t.setX(10.0); t.setY(70.0); t.setFill(Color.RED); t.setText("JavaFX"); t.setFont(Font.font(null, FontWeight.BOLD, 32)); g.getChildren().add(t);  root.getChildren().add(g); primaryStage.setScene(scene); primaryStage.show(); }}

上面的代码生成以下结果。

例子

具有CHOCOLATE颜色和Font.SERIF的文本

import javafx.application.Application;import javafx.scene.Group;import javafx.scene.Scene;import javafx.scene.paint.Color;import javafx.scene.shape.Circle;import javafx.scene.text.Font;import javafx.scene.text.Text;import javafx.stage.Stage;public class Main extends Application { public static void main(String[] args) { Application.launch(args); }  @Override public void start(Stage primaryStage) { primaryStage.setTitle("Title");  final Circle circ = new Circle(40, 40, 30); final Group root = new Group(circ);  final Scene scene = new Scene(root, 800, 400, Color.BEIGE); final Text text1 = new Text(25, 25, "w3cschool.cn"); text1.setFill(Color.CHOCOLATE); text1.setFont(Font.font(java.awt.Font.SERIF, 25)); root.getChildren().add(text1);  primaryStage.setScene(scene); primaryStage.show(); }}

上面的代码生成以下结果。

文本效果

DropShadow 对象基于相对于Text节点的x,y偏移量定位。您可以设置阴影的颜色。

以下代码显示如何使用DropShadow绘制文本。

import javafx.application.Application;import javafx.scene.Group;import javafx.scene.Scene;import javafx.scene.effect.DropShadow;import javafx.scene.paint.Color;import javafx.scene.shape.Circle;import javafx.scene.text.Font;import javafx.scene.text.FontWeight;import javafx.scene.text.Text;import javafx.stage.Stage;public class Main extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle(""); Group root = new Group(); Scene scene = new Scene(root, 300, 250, Color.WHITE); Group g = new Group(); DropShadow ds = new DropShadow(); ds.setOffsetY(3.0); ds.setColor(Color.color(0.4, 0.4, 0.4)); Text t = new Text(); t.setEffect(ds); t.setCache(true); t.setX(10.0); t.setY(70.0); t.setFill(Color.RED); t.setText("JavaFX drop shadow..."); t.setFont(Font.font(null, FontWeight.BOLD, 32)); g.getChildren().add(t);  root.getChildren().add(g); primaryStage.setScene(scene); primaryStage.show(); }}

上面的代码生成以下结果。

例2

使用0.7f调用setFraction()方法基本上指定了我们希望显示70%的反射。

以下代码显示如何使用在文本的反射作用。

import javafx.application.Application;import javafx.scene.Group;import javafx.scene.Scene;import javafx.scene.effect.Reflection;import javafx.scene.paint.Color;import javafx.scene.text.Font;import javafx.scene.text.FontWeight;import javafx.scene.text.Text;import javafx.stage.Stage;public class Main extends Application { public static void main(String[] args) { Application.launch(args); }  @Override public void start(Stage primaryStage) { primaryStage.setTitle(""); Group root = new Group(); Scene scene = new Scene(root, 300, 250, Color.WHITE);  Text t = new Text(); t.setX(10.0); t.setY(50.0); t.setCache(true); t.setText("Reflections on JavaFX..."); t.setFill(Color.RED); t.setFont(Font.font(null, FontWeight.BOLD, 30)); Reflection r = new Reflection(); r.setFraction(0.7); t.setEffect(r);  root.getChildren().add(t);  primaryStage.setScene(scene); primaryStage.show(); }}

反射值范围从零(0%)到一(100%)。

我们还可以通过setTopOffset()方法设置不透明节点部分和反射部分之间的空间。顶部偏移默认为零。

上面的代码生成以下结果。

例3

以下代码显示如何使用行分隔符对文本执行换行。

import javafx.application.Application;import javafx.beans.property.SimpleStringProperty;import javafx.beans.property.StringProperty;import javafx.scene.Group;import javafx.scene.Scene;import javafx.scene.effect.InnerShadow;import javafx.scene.effect.InnerShadowBuilder;import javafx.scene.paint.Color;import javafx.scene.text.Font;import javafx.scene.text.FontWeight;import javafx.scene.text.Text;import javafx.scene.text.TextBuilder;import javafx.stage.Stage;public class Main extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Keyboard"); Group root = new Group(); Scene scene = new Scene(root, 530, 300, Color.WHITE); final StringProperty statusProperty = new SimpleStringProperty(); InnerShadow iShadow = InnerShadowBuilder.create() .offsetX(3.5f) .offsetY(3.5f) .build(); final Text status = TextBuilder.create() .effect(iShadow) .x(100) .y(50) .fill(Color.LIME) .font(Font.font(null, FontWeight.BOLD, 35)) .translateY(50) .build(); status.textProperty().bind(statusProperty); statusProperty.set("LineLine2Line"); root.getChildren().add(status);  primaryStage.setScene(scene); primaryStage.show(); }}

上面的代码生成以下结果。

例4

以下代码显示如何设置文本换行宽度。

import javafx.application.Application;import javafx.scene.Group;import javafx.scene.Scene;import javafx.scene.text.Font;import javafx.scene.text.Text;import javafx.stage.Stage;public class Main extends Application { @Override public void start(Stage stage) { Group root = new Group(); Scene scene = new Scene(root, 300, 150); stage.setScene(scene); stage.setTitle("Sample"); Text t = new Text(10, 50, "This is a test"); t.setWrappingWidth(200); t.setText("First row Second row Second row Second row Second row Second row "); t.setFont(new Font(20)); root.getChildren().add(t); stage.show(); } public static void main(String[] args) { launch(args); }}

上面的代码生成以下结果。

javafx label设置字体大小_JavaFX-实现文本相关推荐

  1. javafx label设置字体大小_如何把智能手机,设置成老年人模式?

    hello大家好!我是方脸鸭! 如果你买了新的手机,那么旧的手机无非就是放闲鱼,或者拿给家里的长辈们使用.那么方脸鸭今天就跟大家分享,如何把智能手机设置得更符合家里的长辈使用. [详情请看视频讲解] ...

  2. Qt的label设置字体大小

    跳转到总目录.. 目录 方法一(使用ui布局): 方法二(使用代码): 方法一(使用ui布局): 选中label控件 搜索"font" 修改point size后面的值 方法二(使 ...

  3. QT 中 界面中消息的停留时间解决方案 以及 label 中字体大小和换行设置

    QT 中 界面中消息的停留时间解决方案 以及 label 中字体大小和换行设置 参考文章: (1)QT 中 界面中消息的停留时间解决方案 以及 label 中字体大小和换行设置 (2)https:// ...

  4. java label设置字体颜色_Java中怎么设置JLabel的字体样式,大小,颜色

    展开全部 答 : 最常32313133353236313431303231363533e4b893e5b19e31333363386166见的解决方案有两种. 第一种: 原生的java写法. 核心代码 ...

  5. python使用matplotlib画图,绘制三维、二维曲线。设置字体大小以及坐标系间距等

    话不多说,直接看代码和效果(不设置字体大小.逐个设置以及批量设置),其中,曲线的大小.类型以及颜色可以看我的另一篇博客. import matplotlib.pyplot as plt from mp ...

  6. latex如何设置字体并加粗_Latex设置字体大小,加粗,加下划线,变斜体_孩纸气_新浪博客...

    Latex 设置字体大小命令由小到大依次为: \tiny \scriptsize \footnotesize \small \normalsize \large \Large \LARGE \huge ...

  7. html怎么设置网页的大小怎么设置,css怎么设置字体大小

    前端开发中,为了突出一些内容常常对一些内容进行字体大小的设置.那么怎么设置字体大小?下面本篇文章就来给大家介绍一下使用CSS设置字体大小的方法,希望对大家有所帮助. 在CSS中,可以使用font-si ...

  8. div html表格样式设置字体大小,css样式表中如何修改字体大小为18px?

    css样式表中如何修改字体大小为18px?下面本篇文章给大家介绍一下.有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助. css样式表中如何修改字体大小为18px? 在css样式表中可以 ...

  9. HTML设置字体大小自适应屏幕与echarts图表颜色根据数据大小实时刷新图表颜色

    设置html文字大小根据页面大小自适应,在使用rem之前是使用的px和百分比,后来发现页面缩放或在小屏幕的电脑显示不尽人意,后改用rem,写法如:font-size: .21rem;或font-siz ...

  10. android html字体大小,android Html.fromHtml font 标签支持设置字体大小和颜色

    由于在android 中的Html源码中对html标签的支持不是很完全,在使用textview加载html自定义字体样式的时候遇到坑了,就是font标签不支持size属性,查看源码中发现没有去解析si ...

最新文章

  1. Android深入浅出系列之实例应用—简单的手指拖动图片,图片滑来滑去显示应用Gallery和BaseAdapter以及ImageView的使用...
  2. 个人网站搭建---godaddy域名+freewebhostingarea免费空间
  3. YOLOX——Win10下训练自定义VOC数据集
  4. oracle自带split函数_Pandas 基本使用(三) — DataFrame.to_dict() 函数基本使用
  5. spotify音乐下载_使用Python和R对音乐进行聚类以在Spotify上创建播放列表。
  6. day32—CSS多列布局学习
  7. scorm课件学习状态
  8. struts2随笔(一)Action、struts.xml、Interceptor细节
  9. redis 公网 安全_redis配置之安全配置
  10. python有什么用-python有什么用
  11. python白鹅类型_fluent python 11.10节 鹅的行为有可能像鸭子
  12. 一文掌握 Docker 技术体系
  13. 英语应用文写作之道歉信
  14. JSP中EL表达式和JSTL标签库的使用
  15. FFMPEG 编解码失败 non-existing PPS 0 referenced
  16. Spring AOP tx:advice
  17. 气传导蓝牙耳机哪个牌子好?口碑好的气传导耳机分享
  18. 小白学机器学习西瓜书-第三章对数几率回归
  19. app注册协议做法和注意事项
  20. 五分钟搞定:Centos中Kafka和Zookeeper的快速安装教程

热门文章

  1. 和周杰讨论:DB2连接问题
  2. mybatis-plus环境搭建
  3. 存储 angularjs
  4. Could not find a price list in Ordered UOM xxx and Primary UOM of the item
  5. 2015年总结,平平淡淡的一年.
  6. Android 项目规范 1
  7. Mysql NDB Cluster搭建测试
  8. Minecraft 1.12.2 彩色渐变字体0.5 RGB光束+光影效果
  9. 直播第三方美颜sdk是什么?
  10. 她在IT圈里摸爬滚打的十年