JavaFX文本

news2024/10/6 12:24:31

另一个基本的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");
        //FontWeight:粗细
        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 {
    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("Title");

        Circle circ = new Circle(40, 40, 30);
        Group root = new Group(circ);

        Scene scene = new Scene(root, 800, 400, Color.BEIGE);

        Text text1 = new Text(25, 25, "HelloWorld!");
        text1.setFill(Color.CHOCOLATE);
        text1.setFont(Font.font("宋体", 25));
        root.getChildren().add(text1);

        stage.setScene(scene);
        stage.show();

    }

    public static void main(String[] args) {
        launch();
    }

}

文字效果

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, 400, 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, 400, 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(0%)到1(100%)。还可以通过setTopOffset()方法设置不透明节点部分和反射部分之间的空间。顶部偏移默认为0。
上面的代码生成以下结果。

实例-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.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("Keyboard");
        Group root = new Group();
        Scene scene = new Scene(root, 530, 300, Color.WHITE);

        StringProperty statusProperty = new SimpleStringProperty();

        InnerShadow iShadow = new InnerShadow();
        iShadow.setOffsetX(3.5);
        iShadow.setOffsetY(3.5);

        Text status = new Text();
        status.setEffect(iShadow);
        status.setX(100);
        status.setY(50);
        status.setFill(Color.LIME);
        status.setFont(Font.font(null, FontWeight.BOLD, 35));
        status.setTranslateY(50);

        status.textProperty().bind(statusProperty);
        statusProperty.set("Line\nLine2\nLine3");
        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);
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1829424.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

稀疏矩阵是什么 如何求

稀疏矩阵是一种特殊类型的矩阵,其中大多数元素都是零。由于稀疏矩阵中非零元素的数量远少于零元素,因此可以使用特定的数据结构和算法来高效地存储和处理它们,从而节省存储空间和计算时间。 RowPtr 数组中的每个元素表示对应行的第一个非零元…

计算机缺失msvcr110.dll如何解决,这6种解决方法可有效解决

电脑已经成为我们生活和工作中不可或缺的工具,然而在使用电脑的过程中,我们常常会遇到一些问题,其中之一就是电脑找不到msvcr110.dll文件。这个问题可能会给我们带来一些困扰,但是只要我们了解其原因并采取相应的解决方法&#xf…

C 语言连接MySQL 数据库

前提条件 本机安装MySQL 8 数据库 整体步骤 第一步:开启Windows 子系统安装Ubuntu 22.04.4,安装MySQL 数据库第三方库执行 如下命令: sudo aptitude install libmysqlclient-dev wz2012LAPTOP-8R0KHL88:/mnt/e/vsCode/cpro$ sudo aptit…

使用Java Spring Boot生成二维码与条形码

个人名片 🎓作者简介:java领域优质创作者 🌐个人主页:码农阿豪 📞工作室:新空间代码工作室(提供各种软件服务) 💌个人邮箱:[2435024119qq.com] &#x1f4f1…

导出excle表

文章目录 导出excle表需求场景引入依赖具体代码 导出excle表 需求场景 假设我们有一个需求,现在数据库中有一些用户信息,我们想要把这些信息导出到excle表格中,然后存储到本地磁盘中。要求:excle表格的第一行需要有黄色背景&…

系统报错vcruntime140_1.dll文件缺失怎么回事?多种解决方法让你对比

一、vcruntime140_1.dll常见问题与错误信息 错误信息类型 启动错误:应用程序在启动时提示缺少 vcruntime140_1.dll 文件。 运行时错误:应用程序在运行过程中突然崩溃,提示 vcruntime140_1.dll 错误。 兼容性错误:新旧版本的 V…

7z及7zip-cpp最高压缩比的免费开源压缩软件

7z介绍 7z是一种主流高效的压缩格式,它拥有极高的压缩比。在计算机科学中,7z是一种可以使用多种压缩算法进行数据压缩的档案格式。该格式最初由7-Zip实现并采用,但这种档案格式是公有的,并且7-Zip软件本身亦在GNU宽通用公共许可证…

js 前端 Function.prototype.call.call(0[‘toString‘], *, 16)

这个函数将 数组转任意进制 Function.prototype.call.call(0[toString], *, 16)

小工具开发

因不太喜欢重复性工作,为了提高日常工作效率,在业余时间开发一些小工具用于帮助自己“偷懒”。 小工具功能: 1、Hightec编译的hex文件,与多模式标定hex文件合成 2、Bootloader文件,Hightec编译的hex文件,与…

Qt画五角星,简单图表

五角星&#xff1a; 代码&#xff1a; widget.cpp #include "widget.h" #include "ui_widget.h" #include <QPaintEvent> #include <QPainter> #include <QPainterPath> Widget::Widget(QWidget *parent): QWidget(parent), ui(new U…

Mysql学习笔记-SQL优化总结

详细内容参见https://blog.csdn.net/qingwufeiyang_530/article/details/139705898?csdn_share_tail%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22139705898%22%2C%22source%22%3A%22qingwufeiyang_530%22%7D

Python学习笔记11:入门终结篇

前言 入门知识到这里基本结束了&#xff0c;这里主要讲一下input和range。这两个讲完&#xff0c;讲讲后面进阶学些啥。 range函数 之前将循环的时候讲过一点&#xff0c;这个函数是Python内置的函数&#xff0c;主要用来生成一系列数字&#xff0c;简单方便。 这里重新&…

实践分享:鸿蒙跨平台开发实例

先来理解什么是跨平台 提到跨平台&#xff0c;要先理解什么是“平台”&#xff0c;这里的平台&#xff0c;就是指应用程序的运行环境&#xff0c;例如操作系统&#xff0c;或者是Web浏览器&#xff0c;具体的像HarmonyOS、Android、iOS、或者浏览器&#xff0c;都可以叫做平台…

HTTP协议版本历程

HTTP协议的发展历程 版本推出年份当前状态HTTP/0.91991年已过时HTTP/1.01996年已过时HTTP/1.11997年标准HTTP/2.02015年标准HTTP/3.02022年标准 HTTP/0.9 HTTP/0.9非常简单&#xff0c;并不涉及数据包传输&#xff0c;通过请求和响应的交换达成通信&#xff0c;请求由单行指…

【猫狗分类】Pytorch VGG16 实现猫狗分类5-预测新图片

背景 好了&#xff0c;现在开尝试预测新的图片&#xff0c;并且让vgg16模型判断是狗还是猫吧。 声明&#xff1a;整个数据和代码来自于b站&#xff0c;链接&#xff1a;使用pytorch框架手把手教你利用VGG16网络编写猫狗分类程序_哔哩哔哩_bilibili 预测 1、导包 from to…

DataWhale - 吃瓜教程学习笔记(一)

学习视频&#xff1a;第1章-绪论_哔哩哔哩_bilibili 西瓜书对应章节&#xff1a; 第一章 机器学习三观 What&#xff1a;什么是机器学习&#xff1f; 关键词&#xff1a;“学习算法” Why: 为什么要学机器学习&#xff1f; #### 1. 机器学习理论研究#### 2. 机器学习系统开…

.net8 blazor auto模式很爽(五)读取sqlite并显示(2)

在BlazorApp1增加文件夹data&#xff0c;里面增加类dbcont using SharedLibrary.Models; using System.Collections.Generic; using Microsoft.EntityFrameworkCore;namespace BlazorApp1.data {public class dbcont : DbContext{public dbcont(DbContextOptions<dbcont>…

Servlet基础(续集2)

HttpServletResponse web服务器接收到客户端的http的请求&#xff0c;针对这个请求&#xff0c;分别创建一个代表请求的HttpServletRequest对象&#xff0c;代表响应的一个HttpServletResponse 如果要获取客户端请求过来的参数&#xff1a;找HttpServletRequest如果要给客户端…

梦想编织者Luna:COZE从童话绘本到乐章的奇妙转化

前言 Coze是什么&#xff1f; Coze扣子是字节跳动发布的一款AI聊天机器人构建平台&#xff0c;能够快速创建、调试和优化AI聊天机器人的应用程序。只要你有想法&#xff0c;无需有编程经验&#xff0c;都可以用扣子快速、低门槛搭建专属于你的 Chatbot&#xff0c;并一键发布…

Web前端项目-交互式3D魔方【附源码】

交互式3D魔方 ​ 3D魔方游戏是一款基于网页技术的三维魔方游戏。它利用HTML、CSS和JavaScript前端技术来实现3D效果&#xff0c;并在网页上呈现出逼真的魔方操作体验。 运行效果&#xff1a; 一&#xff1a;index.html <!DOCTYPE html> <html><head><…